什么是Bitmap.Clone()和新的位图(位图)有什么区别? [英] What's the difference between Bitmap.Clone() and new Bitmap(Bitmap)?

查看:1450
本文介绍了什么是Bitmap.Clone()和新的位图(位图)有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,有复制位图的方法有两种。

As far as I can tell, there are two ways of copying a bitmap.

Bitmap.Clone()

Bitmap A = new Bitmap("somefile.png");
Bitmap B = (Bitmap)A.Clone();

新位图()

Bitmap A = new Bitmap("somefile.png");
Bitmap B = new Bitmap(A);

如何将这些方法有什么不同?我在的内存和线程方面的差异尤为感兴趣。

How do these approaches differ? I'm particularly interested in the difference in terms of memory and threading.

推荐答案

它与几乎德$ 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.

这篇关于什么是Bitmap.Clone()和新的位图(位图)有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆