PictureBox 在导航到不同的应用程序时丢失其图像
PictureBox loses its image on navigating to different application
我最近开始开发 Windows 表单应用程序。我正在使用 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
这是因为您正在处理图片
using (Image original = ....
{
Bitmap image1 = (Bitmap)original;
// ...
}
image1
与 original
是同一个对象,只是转换为 Bitmap
,并在绘制到屏幕上后立即处理(使用 Refresh
方法)。
要解决此问题,请改用以下方法
Bitmap image1 = new Bitmap(original);
// ...
我最近开始开发 Windows 表单应用程序。我正在使用 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
这是因为您正在处理图片
using (Image original = ....
{
Bitmap image1 = (Bitmap)original;
// ...
}
image1
与 original
是同一个对象,只是转换为 Bitmap
,并在绘制到屏幕上后立即处理(使用 Refresh
方法)。
要解决此问题,请改用以下方法
Bitmap image1 = new Bitmap(original);
// ...