I believe that this is the effect of PictureBoxSizeMode.Zoom
. The documentation says that:
The size of the image is increased or decreased maintaining the size ratio.
You set this on the PictureBox.SizeMode
property. The "Remarks" section of the documentation for that function also says:
Using the Zoom value causes the image to be stretched or shrunk to fit the PictureBox; however, the aspect ratio in the original is maintained.
You can, of course, set the PictureBox.SizeMode
property either in the designer's Properties Window or in code (e.g., in your form's constructor):
myPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
If this doesn't do exactly what you want, you could always implement the resizing logic yourself. Your concern is that recreating the image in memory each time that the control is resized "seems like a bad idea", but I'm not sure why it seems that way to you. The only problem would be if you weren't careful to destroy unused graphics objects, like the old Bitmap
. Not only do these objects contain unmanaged resources that need to be freed, you'll start exerting extreme amounts of pressure on memory if you just let them leak.
Alternatively, to avoid creating temporary bitmaps, you can do what the PictureBox control probably does internally and use the Graphics.DrawImage
method to handle the stretching. If you pass it a rectangle, it will automatically scale the image to fit inside of the rectangle.