Bitmap deep copy changing PixelFormat

独自空忆成欢 提交于 2019-12-02 18:01:35

问题


Bitmap img = new Bitmap("C:\\temp\\images\\file.jpg");

img.PixelFormat is Format24bppRgb

when I am doing deep copy

Bitmap img2 = new Bitmap(img);

img.PixelFormat is changed to Format32bppArgb

why does it change pixel format? and how to make deep copy for the object if it doesn't make deep copy?


回答1:


You can clone the bitmap like this, which will create a deep copy:

Bitmap img = new Bitmap("C:\\temp\\images\\file.jpg");

// Clone the bitmap.
Rectangle cloneRect = new Rectangle(0, 0, img.Width, img.Height);
System.Drawing.Imaging.PixelFormat format =
    img.PixelFormat;
Bitmap img2 = img.Clone(cloneRect, format);



回答2:


Just found solution instead new Bitmap(img) use Bitmap img2 = (Bitmap) img.Clone(); don't know it is the right solution, but it do the job.



来源:https://stackoverflow.com/questions/20044431/bitmap-deep-copy-changing-pixelformat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!