Changing palette in GDI+

Posted on 2003-10-29
Last Modified: 2012-05-04
How can I change a 16- or 24-bit palette down to a fixed 8-bit color palette so that I can pre-define the colors in the target palette? That is, I have a set of 255 colors and no other colors can be used. The transformation must use some kind of advanced rendering instead of a straight next possible color transformation.

Question by:Keijo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 48

Accepted Solution

AlexFM earned 250 total points
ID: 9640635
This problem is solved in VC++, see the following links:

Maybe somebody implemented this algorithm in .NET. If not, you need to use this code or translate it to C#.
I used CQuantizer code few years ago to display true-color bitmaps in 256-colors display, it gives a good results.

Author Comment

ID: 9641198
Thanks for your answer Alex,

There's still one problem with the CQuantizer, as it generates the optimal palette itself. As I mentioned, I'm stuck with a pre-defined palette that can not be changed. I took a quick look at the source of CQuantizer.ProcessImage and at least I couldn't figure out how to force the palette.

LVL 48

Expert Comment

ID: 9641264
CQuantizer generates optimal color palette from image.
Reading your question again I see that you need very simple algorithm: convert image to some color palette. For each pixel in the image you need to find the closest match in the palette, like GetNearestPaletteIndex API does. This is simple task: compare pixel value with each palette entry. If values are equal, use this entry. If not, continue search and select the closest match.

Author Comment

ID: 9648557
I'm actually developing for a platform that supports only 256 fixed colors, and when other colors are encountered they are automatically converted to the closest match. The problem with this is that the images tend to turn out pretty crappy because this way, so an algorithm that does some more advanced rendering is definitely needed.
LVL 48

Expert Comment

ID: 9648740
Unfortunately, there is no such algorithm. Finding the best match for each pixel is the best possible way. The problem is that current palette is not good for the image. The solution is to find optimal palette for each image, and this is what CQuantizer class does.
Consider the color palette which contains entries (0, 0, 0),  (1,1,1) ... (255,255,255) - gray-scale palette and image with only one color - (255,0,0). Using this palette you cannot draw this simple image correctly.
Old PC graphic cards supported only 256 colors, but programmers could select color palette. Each program could create it's own color palette and activate it when it is in foreground. CQuantizer was written in these old times. If your platform supports 256 colors allowing to change current palette dynamically, use CQuantizer algorithm. If your platform supports only 256 fixed colors, there is nothing to do.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question