Solved

Bitmap Palette

Posted on 1997-05-08
2
1,587 Views
Last Modified: 2008-02-26
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;
0
Comment
Question by:tier
2 Comments
 
LVL 2

Expert Comment

by:gysbert1
ID: 1336022
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
 
LVL 2

Accepted Solution

by:
javiertb earned 25 total points
ID: 1336023
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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