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

99封情书 提交于 2019-12-11 07:49:30

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/18707850/why-the-transparency-of-a-png-image-is-lost-when-i-use-tpicture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!