[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1079
  • Last Modified:

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
0
szafran
Asked:
szafran
1 Solution
 
LMuadDIbCommented:
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
0
 
szafranAuthor Commented:
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 :/
0
 
saravananvgCommented:
Hello Sir,

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

with regards,
padmaja.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
szafranAuthor Commented:
Still no luck with using it :/
0
 
Slick812Commented:
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
0
 
szafranAuthor Commented:
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) ??
0
 
Slick812Commented:
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now