Re: Converting a GDI+ 24-bit RGB Bitmap to an Indexed Format Bitmap

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I would suggest computing a new Palette using one of the many color
reduction algorithms available.
MSDN has the code for the octree quantization algorithm.

Once you have a new palette, create a new bitmap with the same width and
height of your resized image.
Use the original indexed pixel format for this new image.

Since your resized image is 24bpp, you need to translate the 3 colors in
each pixel into an index of your new color palette.
If you create a logical palette via GDI, you may use the function
GetNearestPaletteIndex to get the new index for the pixel.
You use Lockbits, to cycle through each 24bpp pixel and take the index
returned by GetNearestPaletteIndex and set the
corresponding pixel in your indexed image to this index.

..
"Alain M. Dube" <adube@xxxxxxxxxxxxxxxxxxx> wrote in message
news:%23jEldIYyHHA.5024@xxxxxxxxxxxxxxxxxxxxxxx
Hello;

Can someone shed some light on how I can (using GDI+ and/or GDI) convert a
Bitmap whose pixel format is PixelFormat24bppRGB to a bitmap with any
variation of the PixelFormatIndexed pixel format? Note that the source
bitmap is "indexed" and converted to 24-bit RGB, then resized. No color
changes are made. Once completed, I'd like to return the bitmap to it's
original pixel format.

In theory, the source bitmap's palette should be reusable. I thought that
it would be a simple matter of iterating through the 24-bit RGB bitmap
data,
one pixel at a time, attempting to locate the pixel's color in the source
bitmap's palette. The colors values to not seem to match at all. The
color
palette values I see are actually in the source bitmap file. if I save
the
24-bit RGB bitmap to disk, the colors that I see as bitmap pixel data
(while
stepping througnb the code) are present in the saved bitmap file. The
color
values in the palette and in the 24-bit RGB data seem to be incompatible.

Thanks in advance;

Alain.





.



Relevant Pages

  • Re: Converting a GDI+ 24-bit RGB Bitmap to an Indexed Format Bitmap
    ... I created and populated a new palette and an array of indexed pixel ... method to create the new GDI+ "indexed" Bitmap. ... Is a stride necessary with indexed pixel data? ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Image Control/Picture object Palette
    ... If you use LoadImage to load your 16 colour bitmap from your copy of ntoskrnl.exe without specifying any flags, as the vbAccelerator code is doing, then you will end up with a bitmap that has the same colour depth as your screen, which is not what you want. ... The problem in this case however is that you are not loading what could be considered a standard 16 colour bitmap in which the 16 palette entries generally contain various different colour values but you are loading a 16 colour bitmap in which the palette entries used by the bitmap have all been deliberately set to black and the real palette entries that XP will dump into the bitmap just before displaying it are stored elsewhere in ntoskrnl.exe. ... This specific palette causes LoadImage with the LR_CREATEDIBSECTION flag to effectively erase all the pixel data nibbles in the bitmap, setting them all same colour. ...
    (microsoft.public.vb.general.discussion)
  • Re: Converting a GDI+ 24-bit RGB Bitmap to an Indexed Format Bitmap
    ... The easiest method of creating a gdiplus bitmap is to create a memory stream ... I created and populated a new palette and an array of indexed pixel ... Is a stride necessary with indexed pixel data? ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Unexpected Stripes In Bitmap
    ... sure how to get around having the local bitmap file as that's how I'm ... currently getting access to the 16 color palette. ... If you want to store the index, one index (i.e. one pixel) per byte, then   ...
    (microsoft.public.dotnet.languages.csharp)
  • Converting a GDI+ 24-bit RGB Bitmap to an Indexed Format Bitmap
    ... Bitmap whose pixel format is PixelFormat24bppRGB to a bitmap with any ... the source bitmap's palette should be reusable. ... it would be a simple matter of iterating through the 24-bit RGB bitmap data, ...
    (microsoft.public.win32.programmer.gdi)