I’m trying to resize the image after upload but it doesn’t work
What’s wrong?
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog() == DialogResult.OK)
{
var imagem = new Bitmap(open.FileName);
var resizedImage = new Bitmap(imagem, pictureBox1.Size);
pictureBox1.Image = resizedImage;
}
}
PictureBox already has the ability to resize an image.
If you are assigning the image using the
Imageproperty then you can set the SizeMode property to StretchImage. Which means you code would look like this:If you want to assign the image using the
BackgroundImageproperty then you can set the BackgroundImageLayout property to Stretch. Which means you code would look like this:From personal experience I have found the PictureBox control to be a pain. If you are needing to change or remove the image at any point I always found I needed to explicitly dispose of the image or it gave me memory leaks, and in some cases just plain errors. It is worth noting that when you need to do custom drawing or rendering in WinForms then the
Panelcontrol is a much better option. You can draw images onto aPaneltoo, but depending on your needs, you may be find just using thePictureBox