且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是Bitmap.Clone()和新的位图(位图)有什么区别?

更新时间:2022-06-24 14:52:12

它与几乎德$ P $之间的深和浅副本,也是一个问题上的共同差异pcated IClonable接口。克隆()方法创建一个新的位图对象,但像素数据与原来的位图对象共享。位图(图片)构造函数还创建了一个新的位图对象,但一个有自己的像素数据的副本。

It is the common difference between a "deep" and a "shallow" copy, also an issue with the almost-deprecated IClonable interface. The Clone() method creates a new Bitmap object but the pixel data is shared with the original bitmap object. The Bitmap(Image) constructor also creates a new Bitmap object but one that has its own copy of the pixel data.

使用克隆()是的非常的很少有用。大量的SO关于它的问题在哪里,程序员希望克隆()避免使用位图的典型故障,从它被加载的文件锁。它没有。只有当你传递给code基准的部署位图使用的clone(),你不希望失去的对象。

Using Clone() is very rarely useful. Lots of questions about it at SO where the programmer hopes that Clone() avoids the typical trouble with bitmaps, the lock on the file from which it was loaded. It doesn't. Only use Clone() when you pass a reference to code that disposes the bitmap and you don't want to lose the object.