4

This code:

System.Drawing.Bitmap bpp24 = new System.Drawing.Bitmap(3, 3, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Console.WriteLine("bpp24 pixel format is " + bpp24.PixelFormat.ToString());

System.Drawing.Image bpp24image = bpp24;
Console.WriteLine("bpp24image pixel format is " + bpp24image.PixelFormat.ToString());
System.Drawing.Bitmap duplicate = new System.Drawing.Bitmap(bpp24image);
Console.WriteLine("duplicate pixel format is " + duplicate.PixelFormat.ToString());

System.Drawing.Bitmap clone = (System.Drawing.Bitmap)bpp24.Clone();
Console.WriteLine("clone pixel format is " + clone.PixelFormat.ToString());

Produces this output:

bpp24 pixel format is Format24bppRgb

bpp24image pixel format is Format24bppRgb

duplicate pixel format is Format32bppArgb

clone pixel format is Format24bppRgb

Why did duplicate's PixelFormat change? A workaround to truly duplicate a Bitmap seems to be the object-level Clone method as shown last (and the Bitmap-level Clone method, but that takes some extra typing), but why can't the higher-level Bitmap(Image) constructor be used to duplicate the Bitmap?

Also see Bitmap deep copy changing PixelFormat and Converting Bitmap PixelFormats in C#.

Community
  • 1
  • 1
Ben
  • 1,272
  • 13
  • 28
  • Doesn't the second link you've posted more-or-less answer your question? It's probably a bug, deep down in Windows GDI+ processing. Incidentally, I've experienced incorrect pixel depth when loading files from disk with Bitmap, so I've gone over to using ImageConverter. http://stackoverflow.com/questions/3801275/how-to-convert-image-in-byte-array/16576471#16576471 – RenniePet Sep 17 '14 at 22:57
  • Does Microsoft actually acknowledge this as a bug? It could be intended behavior as in http://stackoverflow.com/a/15228818/651139 – Ben Sep 17 '14 at 23:14
  • OK, maybe I'm in error. I was unaware of the PreservePixelFormat option. – RenniePet Sep 17 '14 at 23:17
  • Well, that's for BitmapImage (different namespace) -- I'm just thinking the intention could be similar here – Ben Sep 17 '14 at 23:22

0 Answers0