How to change a color in TBitmaps palette

Posted on 2006-06-04
Last Modified: 2008-03-04

I need to change the 3rd color to white in TBitmaps pallete... How can i do it ?

THX in advance
Question by:szafran
    LVL 4

    Expert Comment

    try "SetDIBColorTable" :

      bmp: TBitmap;
      NewCol: TRGBQuad;
      bmp := TBitmap.Create;

      NewCol.rgbBlue := 255;
      NewCol.rgbGreen := 0;
      NewCol.rgbRed := 0;
      NewCol.rgbReserved := 0;

      SetDIBColorTable(bmp.Canvas.Handle, 255, 3, NewCol);

      Image1.Picture.Graphic:= bmp;

    check out this link for some more info:

    Author Comment

    doesn't work

    UINT SetDIBColorTable(
      HDC hdc,               // handle to DC
      UINT uStartIndex,      // color table index of first entry
      UINT cEntries,         // number of color table entries
      CONST RGBQUAD *pColors // array of color table entries
    [in] Specifies a device context. A DIB must be selected into this device context.
    [in] A zero-based color table index that specifies the first color table entry to set.
    [in] Specifies the number of color table entries to set.
    [in] Pointer to an array of RGBQUAD structures containing new color information for the DIB's color table.

    so i've used:
    SetDIBColorTable(BMP2GIF.Canvas.Handle, 2, 1, Kolor);

    but i don't know how to get:
    [in] Pointer to an array of RGBQUAD structures containing new color information for the DIB's color table.

    because SetDIBColorTable(BMP2GIF.Canvas.Handle, 2, 1, Kolor) doesn't work also :/
    LVL 6

    Expert Comment

    Hello Sir,

      Check the following link for more information

    with regards,

    Author Comment

    Still no luck with using it :/
    LVL 33

    Expert Comment

    hello szafran , I have use the SetDIBColorTable on bitmaps and it always seems to work (except if the bitmap does not have a palette, 24 bit bitmap), here is some button click code below that will change a single palette entry on the  Bmp8Bit  bitmap. -->

    procedure TForm2.ChangePalete1Click(Sender: TObject);
    Bmp8Bit: TBitmap;
    Kolor: Cardinal;
    Bmp8Bit := TBitmap.Create;
      Bmp8Bit.LoadFromFile('E:\8 bit.bmp');
      if Bmp8Bit.PixelFormat <> pf8Bit then
        ShowMessage('ERROR, Bitmap is NOT 8 Bit');
      Kolor := $DA33E7;
      Canvas.Draw(20,70, Bmp8Bit); // show bitmap Before change
      if SetDIBColorTable(Bmp8Bit.Canvas.Handle, 2, 1, Kolor) = 0 then
        ShowMessage('ERROR, System did not change palette, bitmap may be 24 bit');;
      Canvas.Draw(280,70, Bmp8Bit); // show Bmp after palette change to see it

     = = = = = = = = = = = = = = = = = = = =
    it may be that after you change the palette, you are not refreshing the thing that displays your bitmap (maybe a TImage) ? ? and that's why you do not see the change. . .
    also, I cannot tell from your code what the   BMP2GIF  variable is, it should be a TBitmap

    ask questions if you need more info

    Author Comment

    BMP2GIF is a TBitmap var (and my bitmap is 4bit, but shouldn't affect the changing process in any way)
    I'm not displaying the bitmap... it's then saved as a transparent gif using TGIFImage (and the 3rd color is the transparent one, and it's saved as black, and it should be white)

    btw what's the difference between BMP8Bit.Free and FreeAndNil(BMP8Bit) ??
    LVL 33

    Accepted Solution

    OK, I see what may be the problem. . .many of the GIF components have internal code methods to handle the GIF image transparent color, so even if you change the palette entrys of the bitmap used for the GIF output, the transparent "Color" will be automatically set (or reset) to what the GIF component has the setting for the Transparent color, , I beleive that you should set (or reset) the transparent color in the GIF component properties, instead of the palette entry of the TBitmap, , but I am guessing here, since I do do not know of the specific TGIFImage properties you are using. . . . .

    I try and use the FreeAndNil(  ) instead of the   Object.Free  , although they both Free the object, the FreeAndNil will check first to see if the object is nil (not existing) and if it is nil then it does nothing, sometimes preventing a access violation, , however, , you should use the delphi "Help" and just look up FreeAndNil, , there's a ton of information in the delphi Help

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    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…
    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…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 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

    17 Experts available now in Live!

    Get 1:1 Help Now