PictureBox.BackgroundImageLayout change fails

坚强是说给别人听的谎言 提交于 2019-12-13 04:02:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!