Changing palette in GDI+

Posted on 2003-10-29
Medium Priority
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 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

770 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