最近开发图像识别,当对一个图片进行处理后重新保存的时候,报“A generic error occurred in GDI+”错误,百思不得其解
最后发现原因是:
To retain access to the source bits, GDI+ locks any source file, and forces the application to maintain the life of any source stream, for the life of the Bitmap or the Image object.
this.image做为过渡
最后发现原因是:
To retain access to the source bits, GDI+ locks any source file, and forces the application to maintain the life of any source stream, for the life of the Bitmap or the Image object.
解决的方法是加载的时候 用Graphics.DrawImage() 来copy Image到一个新建立的Bitmap 对象,然后对这个对像做其它的操作
Source Code
string fileName = "1.png"; //fileNameCounter.ToString() + ".png";
string imagePath = @"Sample Images\" + fileName;
Bitmap b = Bitmap.FromFile(imagePath) as Bitmap;
this.image = new Bitmap(b.Width, b.Height);
using (Graphics graph = Graphics.FromImage(this.image))
{
graph.DrawImageUnscaled(b, 0, 0);
}
b.Dispose();
this.image做为过渡
string fileName = "1.png"; //fileNameCounter.ToString() + ".png";
string imagePath = @"Sample Images\" + fileName;
this.image.Save(imagePath, ImageFormat.Png);