Salvador
Salvador

Reputation: 16492

Why the transparency of a PNG image is lost when I use TPicture?

I'm using this code to convert a transparent png to a 32 bpp bmp.

var
   Picture : TPicture;
   BMP : TBitmap;    
begin
  Picture := TPicture.Create;
  try
    Picture.LoadFromFile('Foo.png');
     BMP := TBitmap.Create;
     try
       BMP.PixelFormat:=pf32bit;
       BMP.Width := Picture.Width;
       BMP.Height := Picture.Height;
       BMP.Canvas.Draw(0, 0, Picture.Graphic);
       BMP.SaveToFile('Foo.bmp');
       finally
         BMP.Free;
       end;
     finally
       Picture.Free;
      end;
end;

The image is converted to bmp but the transparency is lost, what I'm missing?

Upvotes: 1

Views: 1532

Answers (1)

RRUZ
RRUZ

Reputation: 136441

Try using the Assign method. this will preserve the transparency.

like so.

 BMP := TBitmap.Create;
 try
   BMP.Assign(Picture.Graphic);
   BMP.SaveToFile('Foo.bmp');
 finally
     BMP.Free;
 end;

Upvotes: 4

Related Questions