[英]How to save a png file with transparency?
I am using Barcode Studio 2011 to paint a QR Code into a Graphics32 - TImage32 Component and I want to save it in png format but with the white colour as transparent this I have specified in the OuterColor of Graphics32. 我正在使用Barcode Studio 2011将QR码绘制到Graphics32 - TImage32组件中,我希望以png格式保存它,但是白色是透明的,这是我在Graphics32的OuterColor中指定的。
OnFormCreate I have just OnFormCreate我有
procedure TForm1.FormCreate(Sender: TObject);
begin
psBarcodeComponent1.BarCode := 'some text here...';
end;
and for the moment I have the painting assigned to a Button Click Event 目前我已将绘画分配给按钮点击事件
procedure TForm1.Button8Click(Sender: TObject); // Paint the barcode
var
bmp: TBitmap32;
Coords: TRect;
begin
bmp := TBitmap32.Create;
bmp.SetSize(image.Width, image.Height);
bmp.Canvas.Brush.Color := color;
bmp.Canvas.Rectangle(-1, -1, image.Width+2, image.Height+2);
bmp.DrawMode := dmTransparent;
bmp.OuterColor := clWhite;
// make Coords the size of image
Coords := Rect(0,0,image.Width,image.Height);
psBarcodeComponent1.PaintBarCode(bmp.Canvas, Coords);
image.Bitmap.Assign(bmp);
end;
I am using the Vampyre Imaging Library to convert the Bitmap into PNG Format but I will gladly use any library, function, and advice - I have been trying to do this now for nearly a week! 我正在使用Vampyre成像库将Bitmap转换为PNG格式,但我很乐意使用任何库,功能和建议 - 我一直试图这样做近一个星期! I have read through and re-read the documentation of graphics32 and also of the Vampyre Imaging Library but nothing I try will convert the white to a transparent colour.
我已阅读并重新阅读了graphics32以及Vampyre Imaging Library的文档,但我尝试的任何内容都不会将白色转换为透明色。 I have tried clWhite, clWhite32 and also setting the drawMode to dmBlend and applying the ChromaKey Function all to no avail but plenty frustration, coffee and a little beer also ;)
我已经尝试了clWhite,clWhite32并且还将drawMode设置为dmBlend并且应用ChromaKey功能都无济于事但是很多挫折,咖啡和一点啤酒也;)
This is how I am saving it... 这是我如何保存它...
procedure TForm1.Button7Click(Sender: TObject); // Save with Vampyre Imaging Lib
{ Try to save in PNG format with transparancy }
var
FImage: TSingleImage;
begin
FImage := TSingleImage.Create;
ConvertBitmap32ToImage(image.Bitmap, FImage);
FImage.SaveToFile('VampyreLibIMG.png');
end;
This results in a Black coloured thumbnail and when viewed in Windows Photo Viewer it is completely transparent. 这会产生黑色缩略图,在Windows照片查看器中查看时,它是完全透明的。
I hope that I have provided enough information and that someone is able to help me. 我希望我提供了足够的信息,并且有人能够帮助我。
Chris 克里斯
This approach works for me: 这种方法对我有用:
uses GR32, GR32_PNG, GR32_PortableNetworkGraphic;
var
Y: Integer;
X: Integer;
Png: TPortableNetworkGraphic32;
function IsWhite(Color32: TColor32): Boolean;
begin
Result:= (TColor32Entry(Color32).B = 255) and
(TColor32Entry(Color32).G = 255) and
(TColor32Entry(Color32).R = 255);
end;
begin
with Image321 do
begin
Bitmap.ResetAlpha;
for Y := 0 to Bitmap.Height-1 do
for X := 0 to Bitmap.Width-1 do
begin
if IsWhite(Bitmap.Pixel[X, Y]) then
Bitmap.Pixel[X,Y]:=Color32(255,255,255,0);
end;
Png:= TPortableNetworkGraphic32.Create;
Png.Assign(Bitmap);
Png.SaveToFile('C:\Temp\NowTransparent.png');
Png.Free;
end;
end;
This uses the GR32 PNG library . 这使用GR32 PNG库 。 It's a pretty direct way, setting all white pixels to transparent.
这是一种非常直接的方式,将所有白色像素设置为透明。
PS: Image321
is a TImage32
component, containing my TBitmap32
. PS:
Image321
是一个TImage32
组件,包含我的TBitmap32
。
You haven't specified the Delphi version, but if your delphi version has "PngImage"(I believe it comes with D2009+) the code bellow works perfectly(loaded in Gimp and Windows Photo Viewer, it draws a frame and some text with transparent background, feel free to play with it: 你还没有指定Delphi版本,但是如果你的delphi版本有“PngImage”(我相信它带有D2009 +),代码下面的工作完美(在Gimp和Windows Photo Viewer中加载,它绘制一个框架和一些透明背景的文本) ,随意玩它:
uses
PngImage;
procedure TForm1.OnBtnClick(Sender: TObject);
var
bmp: TBitmap;
png: TPngImage;
begin
bmp := TBitmap.Create;
bmp.Width := 200;
bmp.Height := 200;
bmp.Canvas.Brush.Color := clBlack;
bmp.Canvas.Rectangle( 20, 20, 160, 160 );
bmp.Canvas.Brush.Style := bsClear;
bmp.Canvas.Rectangle(1, 1, 199, 199);
bmp.Canvas.Brush.Color := clWhite;
bmp.Canvas.Pen.Color := clRed;
bmp.Canvas.TextOut( 35, 20, 'Hello transparent world');
bmp.TransparentColor := clWhite;
bmp.Transparent := True;
png := TPngImage.Create;
png.Assign( bmp );
png.SaveToFile( 'C:\test.png' );
bmp.Free;
png.Free;
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.