PictureBox和Dispose
我必须删除一个图像文件,如果它已经存在(覆盖它),而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()
而删除了文件系统上的图像,但pic.Dispose()
中没有显示图像。 也许Dispose()
方法使PictureBox
无效?
您可以将图像读入图片框而不锁定它,如下所示
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 +错误。
新更新的代码符合您的所有要求,如下所示
上述就是C#学习教程:PictureBox和Dispose分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1001756.html