# how to save a 24bits bitmap to 256 color bitmap?

### Question

• Now  I have 24bits bitmap, I want to save it to 256 color or 16 color etc. bitmap.  how can I do it in .Net ? Do I have to construct a palette by myself ? I really hope to using some property or API save it directly.
Thursday, May 31, 2007 12:32 PM

• Removing colors from an image is very hard to do without introducing undesirable artifacts.  GDI+ doesn't try.  You can however convert to 8bpp by playing a trick with the GIF encoder.  Check this thread for code.  It uses dithering to approximate colors, that usually only looks good from at no less than a yard.  No such trick exists for 4bpp, you'd have to purchase a commercial image processing library (like LeadTools) or try the freeware ImageMagick library.
Thursday, May 31, 2007 2:48 PM
• If I am reading you correctly, you want to scale a 24bit number down to an 8bit number. Your end result is going to look horrible because you are scaling 16,777,216 possible colors down to 256 (you will get a big blob scaling down to 16 colors).

Anyway, 256/16777216 = 0.0000152587890625

Use 0.0000152587890625 as your scale factor. Simply multiply your 24bit color by 0.0000152587890625 and it becomes an 8bit color somewhere between 0 and 256.

Example:

16777216 * 0.0000152587890625 = 256

8388608 * 0.0000152587890625  = 128

I hope I understood your question...

Bob

Thursday, May 31, 2007 7:40 PM
• You cannot just multiply a 24 bit pixel with 0.0000152587890625 to get an 8 bit pixel. A 24 bit pixel consist of three completely independant colors - 8 bit red, 8 bit green and 8 bit blue.

The best shoot is probably to convert to the old Netscape palette, which is a color palette with 216 colors, which has 6 levels (00H, 33H, 66H, 99H, CCH and FFH) for each color (R, G, B). There are many links like this one: http://www.webmaster.crevier.org/tags/palette.html , which describes this palette.

Friday, June 01, 2007 9:27 AM

### All replies

• Removing colors from an image is very hard to do without introducing undesirable artifacts.  GDI+ doesn't try.  You can however convert to 8bpp by playing a trick with the GIF encoder.  Check this thread for code.  It uses dithering to approximate colors, that usually only looks good from at no less than a yard.  No such trick exists for 4bpp, you'd have to purchase a commercial image processing library (like LeadTools) or try the freeware ImageMagick library.
Thursday, May 31, 2007 2:48 PM
• If I am reading you correctly, you want to scale a 24bit number down to an 8bit number. Your end result is going to look horrible because you are scaling 16,777,216 possible colors down to 256 (you will get a big blob scaling down to 16 colors).

Anyway, 256/16777216 = 0.0000152587890625

Use 0.0000152587890625 as your scale factor. Simply multiply your 24bit color by 0.0000152587890625 and it becomes an 8bit color somewhere between 0 and 256.

Example:

16777216 * 0.0000152587890625 = 256

8388608 * 0.0000152587890625  = 128

I hope I understood your question...

Bob

Thursday, May 31, 2007 7:40 PM
• ok, now forget about what I said above.

so how to save bitmap to 8bits, 4bits even monochrom bmp in .Net?

I don't think bitmap.save("", )  will do this for me,     where I'm wrong?

Friday, June 01, 2007 3:07 AM
• You cannot just multiply a 24 bit pixel with 0.0000152587890625 to get an 8 bit pixel. A 24 bit pixel consist of three completely independant colors - 8 bit red, 8 bit green and 8 bit blue.

The best shoot is probably to convert to the old Netscape palette, which is a color palette with 216 colors, which has 6 levels (00H, 33H, 66H, 99H, CCH and FFH) for each color (R, G, B). There are many links like this one: http://www.webmaster.crevier.org/tags/palette.html , which describes this palette.

Friday, June 01, 2007 9:27 AM
• This will need to use specific algorithm to complete the conversion with a proper colorful result. After investigation I found this is called Color quantization and with three common used algorithms: Popularity, Median Cut, Octrees. If you are interesting about the algorithm you can find them separately.

Finally, I give up this thought and find a workaround: I can get 4, 8 bits bitmap and then I use my own palette(in my project) and then use:

GetDIBits(hDC, hBitmap, 0, bmp.bmHeight, pDIBPixelData, pBmpInfo, (DWORD)DIB_PAL_COLORS);

be careful the last param, if you want use your own palette it is not DIB_RGB_COLORS, then I successfully use my own palette. The effect of a 24bits to 4bits, is the same as MSPaint. Although not very wonderfull but at least acceptable.

Wednesday, December 05, 2007 9:18 AM
• start paint, open 24bits bitmap, go to file, save as and in save as type search for 256Colors bitmap
Monday, September 10, 2012 10:10 AM
• This is a resurrected thread, but the answer changed with the release of WPF and .NET 3.0.  Today use the FormatConvertedBitmap class.
Monday, September 10, 2012 1:26 PM