位图克隆问题
考虑此用于加载、修改和保存位图图像的代码:
using (Bitmap bmp = new Bitmap("C:\\test.jpg"))
{
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
bmp.Save("C:\\test.jpg");
}
它运行时没有任何异常。 但考虑一下这个:
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+ 中发生一般错误”。 这是怎么回事?对打开的文件有任何类型的锁定吗?如果是这样,为什么第一个块有效?当我们可能需要在内存中编辑主对象或其克隆并且仍然将它们都加载到内存中时,克隆 System.Drawing.Bitmap 的正确代码是什么?
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");
}
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用简单的解决方法加载位图而不使用文件锁定:
You could also load the bitmap without file locking using the simple workaround:
是的,当加载第一个位图对象时,文件被锁定,因此同一文件的
bmpClone.Save()
失败,因为存在逻辑死锁。按文件名打开位图时,该文件在位图的整个生命周期中都会被锁定。如果您使用流,则流必须保持打开状态。
更新:
如果您希望内存中有两个位图以便在您所在方法的范围之外使用,那么您将不会使用
using
块。从文件创建第一个图像,然后克隆它。在整个 UI 生命周期中根据需要使用它们,但请确保在不再需要它们时使用
Dispose()
清理它们,以便释放底层资源。另外,来自 MSDN:
这非常尴尬。如果使用
clone()
创建的对象保留有关图像源的信息(例如原始文件的句柄),或者您在使用 Bitmap 实例时无法以其他方式解锁文件,那么您可能会必须保存到新文件,或从原始文件的临时副本打开。试试这个:
Yes, the file is locked when the first bitmap object is loaded and thus
bmpClone.Save()
to the same file fails because you have a logical deadlock.When opening Bitmaps by filename, the file is locked throughout the life of the Bitmap. If you use a stream, the stream must remain open.
Update:
If you wish to have two bitmaps in memory for use outside of the scope of the method you are in, then you won't be using a
using
block.Create the first image from file, and then clone it. Utilize them as needed throughout your UI lifecycle but make sure you clean them up using
Dispose()
when they are no longer needed so that underlying resources are released.Also, from MSDN:
That's pretty awkward. If the object created using
clone()
keeps information on the image source (e.g. a handle on the original file) or you can't otherwise unlock the file while using the Bitmap instances, then you'll probably have to either save to a new file, or open from a temporary copy of the original.Try this instead:
这就是我复制位图的方式:
## ImageCopyBenchmark ##
图像尺寸:{Width=1024,Height=1024}。
图像像素格式:Format8bppIndexed。
Bitmap.Clone():0,00 毫秒(不是 DeepCopy...相同的像素数据 - 看这里)
Bitmap.Clone() + RotateFlip(进行深层复制):2,02 ms
KernellDllCopyBitmap:0.52 毫秒(最好的!)
MarshalCopyBitmap:2,21 毫秒
This is how I copy Bitmaps:
## ImageCopyBenchmark ##
Image Size: {Width=1024, Height=1024}.
Image PixelFormat: Format8bppIndexed.
Bitmap.Clone(): 0,00 ms (Not a DeepCopy... Same pixel data - look here)
Bitmap.Clone() + RotateFlip (to guet a deep copy): 2,02 ms
KernellDllCopyBitmap: 0,52 ms (THE BEST!)
MarshalCopyBitmap: 2,21 ms
这是我的虚荣方法:
快 10%(取决于位图大小......)
Here is my vanity method:
10% faster (depending on bitmap size...)