Bitmap Palette

The following code (Delphi2) shows a bitmap graphic in a TImage.
The bitmap is generated at runtime inside a buffer.
My problem is that the generated palette is not used by the
bitmap although I assigned it...
Instead the bitmap uses the system color palette.
What's wrong?

var i:byte;
    PalSize:longint;
    pLogPalle:PLogPalette;
    PalleEntry:TPaletteEntry;
    Palle:HPalette;
    buffer:array[1..1024] of byte;

begin
     Image1.Picture.Bitmap.Width:=32;
     Image1.Picture.Bitmap.Height:=32;

     PalSize:=sizeof(TLogPalette) + 256 * sizeof(TPaletteEntry);
     pLogPalle:=MemAlloc(PalSize);
     pLogPalle^.palVersion:=$0300;
     pLogPalle^.palNumEntries:=256;
     {$R-}
     for i:=0 to 255 do
     begin
     pLogPalle^.palPalEntry[i].peRed:=i;
     pLogPalle^.palPalEntry[i].peGreen:=0;
     pLogPalle^.palPalEntry[i].peBlue:=0;
     pLogPalle^.palPalEntry[i].peFlags:=PC_EXPLICIT;
     end;
     {$R+}
     Palle:=CreatePalette(pLogPalle^);
     FreeMem(pLogPalle,PalSize);
     Image1.Picture.Bitmap.Palette:=Palle;

     {Fill the buffer...}

     SetBitmapBits(Image1.Picture.Bitmap.Handle, sizeof(buffer), addr(buffer));
     Image1.Refresh;
end;
LVL 1
tierAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gysbert1Commented:
Are you sure it is using the system palette ?

Remember that windows reserves 20 palette entries for system colors over which you have no control. You really only have 235 palette entries to work with and if you are in 256 color mode windows makes a compromise, colorwise, as to which open window can have how many active palette entries.

You can mark some entries as being important and windows will "TRY" to use them but even that is not garuanteed.

In most cases, for this reason, it is not such a good idea to use palettes directly in windows. There are of course certain apps where it is definately necesarry but palette changing is a completely different story than it in DOS.

Try running your app in 24bit mode. If it uses the system palette then there is something wring in your code and I will have a closer look at it. If it runs ok in 24bit mode you did everything perfectly right but windows didn't feel like using your palette.

It is obvious that each window can have a palette that utilises 256 different colors in 24bit mode because the palette is only used to calc the specific RGB values ...
0
javiertbCommented:
The following code shows how to set a bitmap's palette bitmap. The bitmap is within an image box called Image1.

procedure Form1.MakePalette;
var
  LogPal: TLogPalette;
  hPal: HPalette;
  PalEntry: array[0..255] of TPaletteEntry;
  i: Integer;
begin
  for i := 0 to 235 do
  begin
    PalEntry[i].peRed := i;   // here you set your own values        PalEntry[i].peGreen := i;
    PalEntry[i].peBlue := i;
    PalEntry[i].peFlags := PC_NOCOLLAPSE;
  end;
  LogPal.palVersion := $300;
  LogPal.palNumEntries := 1;

  //create the actual palette
 
  hPal := CreatePalette(LogPal);
  ResizePalette(hPal, 256);
  SetPaletteEntries(hPal, 0, 256, PalEntry);

  //make the palette the bitmap's palette
 
  SelectPalette(Image1.Canvas.Handle, hPal, FALSE);
 
  //make windows use the palette in the image
  RealizePalette(Image1.Canvas.Handle);
end;

If you want to change the palette of a bitmap, you can first
read the current palette of the bitmap using a similar
approach, using GetPaletteEntries (see online help). Then you
can change entries and make the changed palette effective as
described above.
This method is quite simple, but device dependent.
Another method requires a bit more work, but is device independent.It uses a so-called DIB: Device Indepenent Bitmap. I believe there is an article about this on the Borland site (www.borland.com).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.