问题
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.
回答1:
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;
回答2:
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;
来源:https://stackoverflow.com/questions/21959490/picturebox-backgroundimagelayout-change-fails