I set up a simple image viewer with the ability to do some basic image processing. At the moment, I have the problem of keeping the PictureBox centered inside the TabPage all the time, and also keeping the width and size of the photo paper the same as the image. So far I have not been successful.
I have the following code that I call in the form constructor to put it in the center. It works for the first time to center the image:
private void SetPictureBoxOriginalSizeAndLocation(bool makeImageNull = false, DockStyle dockStyle = DockStyle.None) { if (makeImageNull) picBoxView.Image = null; picBoxView.Dock = dockStyle; var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width / 2) / 2); var yPoint = tabImageView.Location.Y; var width = tabImageView.Width / 2; var height = (tabImageView.Height / 2) - toolStripImageView.Height; if (picBoxView.Image == null) return;
But it does not resize the image to its image (you can see the black part, which is the color paint for the image control):

The problem gets worse, as soon as I resize the form, the image position will look on top:

I call the code above in the form of a resize event, but I donβt know why it works when the application starts. It would be nice if someone could tell me what properties I should take care of in order to achieve a well-centered image that will always be as large as its image.
Saeid yazdani
source share