当PictureBox显示图像文件时,如果图像文件已经存在(覆盖它),我必须将其删除。但是,如果我试图删除该文件,它会被PictureBox阻止。所以我写了下面的代码:
if (File.Exists(file))
{
Image _tmp = (Image)current_pic.Image.Clone();
current_pic.Image.Dispose();
current_pic.Dispose();
File.Delete(path);
current_pic.Image = _tmp;
current_pic.Image.Save(file, ImageFormat.Jpeg);
}
else
current_pic.Image.Save(file, ImageFormat.Jpeg); 多亏了pic.Dispose(),文件系统上的图像被删除了,但图像并没有更多地显示在PictureBox中。也许Dispose()方法会使PictureBox无效
发布于 2013-04-18 03:12:04
您可以在不锁定图片的情况下将图片读取到图片框中,如下所示
Image img;
string file = @"d:\a.jpg";
using (Bitmap bmp = new Bitmap(file))
{
img = new Bitmap(bmp);
current_pic.Image = img;
}
if (File.Exists(file))
{
File.Delete(file);
current_pic.Image.Save(file, ImageFormat.Jpeg);
}
else
current_pic.Image.Save(file, ImageFormat.Jpeg);我已经更新了代码,甚至支持保存操作。
而前面的代码即使在链接图像之后也支持删除。流已关闭,这在保存时导致了GDI+错误。
新更新的代码满足您的所有需求,如下所示
https://stackoverflow.com/questions/16067859
复制相似问题