Search code examples
c#bitmappixel

convert 32bpp Bitmap to 16bpp (Grayscale)


I'm creating a Bitmap in C# from an Image object.

The Bitmap I get is 32bpp, how can I transform that to 16bpp?

Bitmap image_bmp;
System.Drawing.Image tmp = (System.Drawing.Image)img.RenderImage(0); //RenderImage creates an object of tyoe System.Drawing.Image
image_bmp = new Bitmap(tmp);

Solution

  • If you run a quick search on google you can find a number of excellent color to grayscale formulas. Take the following from wikipedia as an example:

    Y' =  0.2126 R' + 0.7152 G' + 0.0722 B'
    

    With this in mind the general process in .NET is going to be:

    • Create a new image of the correct dimensions with 16bpp grayscale
    • Use "LockBits" on the original image and the new image and loop over each pixel
    • Unlock both images, then save the new image to a file

    Rather than going into these steps further I found a few other questions from stack and around the web that really delve into the details more. See:

    Best of luck!

    NOTE - I'm assuming your using GDI+. The steps are is similar for Windows Imaging Component (WIC) but the classes and syntax is radically different.