Solved

Bitmap Palette

Posted on 1997-05-08
2
1,564 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now