我最近开始开发Windows Forms Applications。我正在使用PictureBox,我正面临着一个问题。当我向外导航或者如果我将其最小化并将其打开时,它正在丢失图像,如下图所示。任何帮助是极大的赞赏。
private void button1_Click(object sender, EventArgs e) {
try {
using (FileStream fs = new FileStream("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg", FileMode.Open, FileAccess.Read)) {
using (Image original = Image.FromStream(fs)) {
Bitmap image1 = (Bitmap)original;
pictureBox1.Image = image1;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Refresh();
}
}
}
catch (System.IO.FileNotFoundException) {
MessageBox.Show("There was an error opening the bitmap." +
"Please check the path.");
}
}
Forms Application before Navigating
Forms Application after Navigating to another app or minimizing it
答案 0 :(得分:1)
这是因为您正在处理图像
using (Image original = ....
{
Bitmap image1 = (Bitmap)original;
// ...
}
image1
与original
的对象相同,只是强制转换为Bitmap
,并在屏幕上绘制后立即处理(使用Refresh
方法)。
要解决此问题,请使用以下内容
Bitmap image1 = new Bitmap(original);
// ...