问题位图克隆 [英] Problem with Bitmap Cloning
问题描述
考虑这个code装载,修改和保存位图图像:
Consider this code for loading, modifying and saving a Bitmap image:
using (Bitmap bmp = new Bitmap("C:\\test.jpg"))
{
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
bmp.Save("C:\\test.jpg");
}
运行没有任何异常。
但考虑这个:
it runs without any exception. But consider this one:
using (Bitmap bmp = new Bitmap("C:\\test.jpg"))
{
using (Bitmap bmpClone = (Bitmap)bmp.Clone())
{
//You can replace "bmpClone" in the following lines with "bmp",
//exception occurs anyway
bmpClone.RotateFlip(RotateFlipType.Rotate180FlipNone);
bmpClone.Save("C:\\test.jpg");
}
}
它结束与该消息的ExternalException:GDI +中发生一般性错误。
什么是错在这里?任何一种在打开的文件锁?如果是这样,为什么第一个块的工作?什么是正确的code克隆一个System.Drawing.Bitmap,而我们可能需要编辑的主要对象或其克隆在内存中,仍然有他们两个在内存中加载?
It ends in an ExternalException with this message: "A generic error occurred in GDI+". What's wrong here? Any kind of lock on opened file? If so, why the first block works? What is the proper code for cloning a System.Drawing.Bitmap while we may need to edit main object or its clone in the memory and still have them both loaded in memory?
推荐答案
您也可以载入位图文件,而不使用简单的解决方法锁定:
You could also load the bitmap without file locking using the simple workaround:
using (Stream s = File.OpenRead(@"\My Documents\My Pictures\Waterfall.jpg"))
Bitmap _backImage = (Bitmap)Bitmap.FromStream(s);
这篇关于问题位图克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!