Question

I am using Visual Studio 2010 and writing a simple project in c#. I have a picture box and two buttons. When one button is pressed, the image in picture box is changed, but I cannot change the background image layout property. In button callback is something like:

pictureBox1.BackgroundImage = Image.FromFile("test.jpg");  
pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;

The image is changed, but it is not stretched over picture box. In fact, only part of the image that fits in picture box is shown.

Any suggestions?

UPDATE

It was my mistake. The call in button callback was actually:

pictureBox1.Image = Image.FromFile("test.jpg");
pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;

instead of upper statement.

No correct solution

OTHER TIPS

private void button1_Click(object sender, EventArgs e)
{
    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    pictureBox1.Image = Image.FromFile("D:/elefent.jpg");      
}

The pictureBox layout changed when I used:

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

If you want to enlarge and fit the image within the control's client rectangle, set the BackgroundImageLayout to ImageLayout.Zoom

pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

You could also try SizeMode property

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

After struggling with this for a few days, This is the only correct answer:

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;

This is not right:

pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top