将像素格式更改为 pf1Bit。创建一个包含 2 个条目的调色板,将 TPaletteEntry 的值更改为所需的颜色值(在所示示例中为红色)。创建一个 TPNGImage,分配位图并设置 PNG 的透明度。
implementation
uses pngimage;
{$R *.dfm}
Type
TMyPalette = Packed Record
palVersion : Word;
palNumEntries : Word;
palPalEntry : Array [0..1] of TPaletteEntry;
End;
Procedure ChangeBlackColor(bmp:TBitMap);
var
pal:TMyPalette;
begin
bmp.PixelFormat := pf1Bit;
bmp.HandleType := bmDIB;
With pal Do
Begin
palVersion:=$0300;
palNumEntries:=2;
palPalEntry[0].peRed:= $FF;
palPalEntry[0].peGreen:=$00;
palPalEntry[0].peBlue:= $00;
palPalEntry[0].peFlags:=PC_RESERVED;
palPalEntry[1].peRed:= $FF;
palPalEntry[1].peGreen:=$FF;
palPalEntry[1].peBlue:= $FF;
palPalEntry[1].peFlags:=PC_RESERVED;
End;
bmp.Palette := CreatePalette(pLogPalette(@pal)^)
end;
procedure TForm3.Button1Click(Sender: TObject);
var
png:TPngimage;
bmp:TBitmap;
begin
// sample image
Image1.Canvas.Rectangle(0,0,Image1.Width-1,Image1.Height-1);
Image1.Canvas.Ellipse(1,1,Image1.Width,Image1.Height);
bmp := Image1.Picture.Bitmap;
ChangeBlackColor(bmp);
png:=TPngimage.Create;
try
png.Assign(bmp);
png.TransparentColor := clWhite;
png.Transparent := true;
Image2.Picture.Assign(png);
finally
png.Free;
end;
end;