tags:

views:

184

answers:

2

I need to convert a Bitmap from PixelFormat.Format32bppRgb to PixelFormat.Format32bppArgb.

I was hoping to use Bitmap.Clone, but it does not seem to be working.

Bitmap orig = new Bitmap("orig.bmp");
Bitmap clone = orig.Clone(new Rectangle(0,0,orig.Width,orig.Height), PixelFormat.Format24bppArgb);

If I run the above code and then check clone.PixelFormat it is set to PixelFormat.Format32bppRgb. What is going on/how do I covert the format?

Thanks.

+2  A: 
using (var bmp = new Bitmap(width, height, PixelFormat.Format24bppArgb))
using (var g = Graphics.FromImage(bmp)) {
  g.DrawImage(..);
}

Should work like that. Maybe you want to set some parameters on g to define the interpolation mode for quality etc.

Benjamin Podszun
+2  A: 

Sloppy, not uncommon for GDI+. This fixes it:

        Bitmap orig = new Bitmap(@"c:\temp\24bpp.bmp");
        Bitmap clone = new Bitmap(orig.Width, orig.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
        using (Graphics gr = Graphics.FromImage(clone)) {
            gr.DrawImage(orig, new Rectangle(0, 0, clone.Width, clone.Height));
        }
        // Dispose orig as necessary..
Hans Passant