Link to home
Start Free TrialLog in
Avatar of szafran
szafran

asked on

How to change a color in TBitmaps palette

Hi

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

THX in advance
Avatar of LMuadDIb
LMuadDIb
Flag of United States of America image

try "SetDIBColorTable" :

Var
  bmp: TBitmap;
  NewCol: TRGBQuad;
begin
  bmp := TBitmap.Create;
  bmp.LoadFromFile('test.bmp');

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

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

  Image1.Picture.Graphic:= bmp;
  bmp.Free;
end;

check out this link for some more info:
http://www.efg2.com/Lab/Library/Delphi/Graphics/Color.htm
Avatar of szafran
szafran

ASKER

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
);
Parameters
hdc
[in] Specifies a device context. A DIB must be selected into this device context.
uStartIndex
[in] A zero-based color table index that specifies the first color table entry to set.
cEntries
[in] Specifies the number of color table entries to set.
pColors
[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 :/
Hello Sir,

  Check the following link for more information
 http://www.efg2.com/Lab/ImageProcessing/Scanline.htm

with regards,
padmaja.
Avatar of szafran

ASKER

Still no luck with using it :/
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);
var
Bmp8Bit: TBitmap;
Kolor: Cardinal;
begin
Bmp8Bit := TBitmap.Create;
  try
  Bmp8Bit.LoadFromFile('E:\8 bit.bmp');
  if Bmp8Bit.PixelFormat <> pf8Bit then
    begin
    ShowMessage('ERROR, Bitmap is NOT 8 Bit');
    Exit;
    end;
  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
  finally
  FreeAndNil(Bmp8Bit);
  end;
end;

 = = = = = = = = = = = = = = = = = = = =
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
Avatar of szafran

ASKER

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) ??
ASKER CERTIFIED SOLUTION
Avatar of Member_2_248744
Member_2_248744
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial