My task is:
- Create a TBitmap object.
- Fill it with transparent color (alpha = 0).
- Assign this bitmap to TPngImage.
- Save PNG file with alpha transparency.
How can I do it in Delphi XE?
var
Png: TPngImage;
X, Y: Integer;
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create();
Bitmap.PixelFormat := pf32bit;
Png := TPngImage.Create();
try
Bitmap.SetSize(100, 100);
// How to clear background in transparent color correctly?
// I tried to use this, but the image in PNG file has solid white background:
for Y := 0 to Bitmap.Height - 1 do
for X := 0 to Bitmap.Width - 1 do
Bitmap.Canvas.Pixels[X, Y]:= $00FFFFFF;
// Now drawing something on a Bitmap.Canvas...
Bitmap.Canvas.Pen.Color := clRed;
Bitmap.Canvas.Rectangle(20, 20, 60, 60);
// Is this correct?
Png.Assign(Bitmap);
Png.SaveToFile('image.png');
finally
Png.Free();
Bitmap.Free();
end;
end;
TCanvas.Pixels
, it's terribly slow and evil ;-) UseTBitmap.Scanline
instead. – TLamaBitmap.TransparentColor
andBitmap.Transparent
after filling background. Can you add this as answer please? – AndrewTPNGImage
by usingCreateBlank
not byCreate
to make its canvas drawable. – TLama