更新
我使用下面的解决方案(从流加载图像),但得到新的问题.img对象绝对正确Image类实例,所有字段都填充了正确的值.但是打电话
img.Save("path/to/new/image.bmp");
Run Code Online (Sandbox Code Playgroud)
在它上面导致GDI +的新异常(System.Runtime.InteropServices.ExternalException,在GDI +接口中) - 我收到错误消息但是很好,我不知道如何翻译它.
原始问题
我有C#.NET Framework 2.0的问题
基本上我试图实现:
Image img = Image.FromFile("Path/To/Image.bmp");
File.Delete("Path/To/Image.bmp"); // Exception, the file is in use!
Run Code Online (Sandbox Code Playgroud)
删除原始文件时,将图像副本保留在内存中非常重要.我虽然有点奇怪,.NET仍然锁定硬盘上的文件,尽管它不再需要任何操作(整个图像现在在内存中,不是吗?)
所以我试过这个解决方案:
Image img = new Image(Image.FromFile("Path/To/Image.bmp")); // Make a copy
// this should immiedietaly destroy original loaded image
File.Delete("Path/To/Image.bmp"); // Still exception: the file is in use!
Run Code Online (Sandbox Code Playgroud)
我可以:
Image img = null;
using(Image imgTmp = Image.FromFile("Path/To/Image.bmp"))
{
img = new Bitmap(imgTmp.Width, imgTmp.Height, imgTmp.PixelFormat);
Graphics gdi = Graphics.FromIage(img);
gdi.DrawImageUnscaled(imgTmp, 0, 0);
gdi.Dispose();
imgTmp.Dispose(); // just …Run Code Online (Sandbox Code Playgroud)