Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert 24-bit bmp to 16-bit?

Tags:

c#

image

I know that the .NET Framework comes with an image conversion class (the System.Drawing.Image.Save method).

But I need to convert a 24-bit (R8G8B8) bitmap image to a 16-bit (X1R5G5B5) and I really got no idea on this kind of conversion, and a 24-to-16-bit change in the bmp header wouldn't work (since we need to convert the entire image data).

Also I would like to know if I can control over the image Dither, etc.

Ideas? Any kind of help would be appreciated.

like image 437
Yana D. Nugraha Avatar asked Mar 04 '10 11:03

Yana D. Nugraha


2 Answers

The Format16bppRgb1555 pixel format is declared but GDI+ doesn't actually support it. There is no main-stream video driver or image codec that ever used that pixel format. Something that the GDI+ designers guessed could have happened, their time machine wasn't accurate enough. Otherwise a pretty sloppy copy/paste from the programmer that worked on System.Drawing.

Rgb555 is the closest match for available hardware and codecs:

public static Bitmap ConvertTo16bpp(Image img) {
    var bmp = new Bitmap(img.Width, img.Height,
                  System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
    using (var gr = Graphics.FromImage(bmp))
        gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
    return bmp;
}
like image 61
Hans Passant Avatar answered Oct 05 '22 23:10

Hans Passant


You need to save the bitmap with an Encoder parameter specifying color depth.

    myEncoder = Encoder.ColorDepth;
    myEncoderParameters = new EncoderParameters(1);

    // Save the image with a color depth of 24 bits per pixel.
    myEncoderParameter = new EncoderParameter(myEncoder, 24L);
    myEncoderParameters.Param[0] = myEncoderParameter;

    myBitmap.Save("MyBitmap.bmp", myImageCodecInfo, myEncoderParameters);
like image 25
Dead account Avatar answered Oct 06 '22 00:10

Dead account