0
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?

Reno
  • 1,291
  • 4
  • 17
  • 29
  • possible duplicate of [How to create a Bitmap deep copy](http://stackoverflow.com/questions/5882815/how-to-create-a-bitmap-deep-copy) – JMK Nov 18 '13 at 09:40

2 Answers2

3

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);
geedubb
  • 4,048
  • 4
  • 27
  • 38
  • I'm suffering from [https://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream](a generic error occured in GDI+). I was able to get around it by creating a copy of the bitmap. But now I notice it changes the PixelFormat unexpectedly. This solution doesn't solve the problem, unfortunately... – Katjoek Mar 01 '19 at 15:45
1

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.

Reno
  • 1,291
  • 4
  • 17
  • 29