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

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

问题描述

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

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();

new Bitmap()

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.

推荐答案

这是深"副本和浅"副本之间的共同区别,也是几乎已弃用的 IClonable 接口的一个问题.Clone() 方法创建一个新的 Bitmap 对象,但像素数据与原始位图对象共享.Bitmap(Image) 构造函数还创建了一个新的 Bitmap 对象,但该对象拥有自己的像素数据副本.

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.

很多关于 Clone() 的问题在 SO 上,程序员希望它避免位图的典型问题,锁定加载它的文件.它没有.一个可能的实际用法是避免库方法在传递的位图上不恰当地调用 Dispose() 的麻烦.

Lots of questions about Clone() at SO where the programmer hopes that it avoids the typical trouble with bitmaps, the lock on the file from which it was loaded. It doesn't. A possibly practical usage is avoiding trouble with a library method that inappropriately calls Dispose() on a passed bitmap.

重载可能很有用,利用像素格式转换或裁剪选项.

The overloads may be useful, taking advantage of the pixel format conversion or the cropping options.

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

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