Image 如何以透明方式保存png文件?
我正在使用Barcode Studio 2011将二维码绘制到Graphics32-TImage32组件中,我想将其保存为png格式,但白色是透明的,这是我在Graphics32的外部颜色中指定的 我刚刚Image 如何以透明方式保存png文件?,image,delphi,png,save,transparency,Image,Delphi,Png,Save,Transparency,我正在使用Barcode Studio 2011将二维码绘制到Graphics32-TImage32组件中,我想将其保存为png格式,但白色是透明的,这是我在Graphics32的外部颜色中指定的 我刚刚 procedure TForm1.FormCreate(Sender: TObject); begin psBarcodeComponent1.BarCode := 'some text here...'; end; 现在我把这幅画分配给了一个按钮点击事件 procedure TForm
procedure TForm1.FormCreate(Sender: TObject);
begin
psBarcodeComponent1.BarCode := 'some text here...';
end;
现在我把这幅画分配给了一个按钮点击事件
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;
我正在使用Vampyre图像库将位图转换为PNG格式,但我很乐意使用任何库、函数和建议-我已经尝试了近一周!我已经通读并重新阅读了graphics32以及Vampyre Imaging Library的文档,但我没有尝试将白色转换为透明颜色。我尝试过clWhite、clWhite32,还将drawMode设置为dmBlend,并应用ChromaKey功能,但都没有效果,但也有很多挫折感、咖啡和一点啤酒;)
这就是我保存它的方式
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;
这会生成黑色缩略图,在Windows照片查看器中查看时,它是完全透明的
我希望我已经提供了足够的信息,并且有人能够帮助我
Chris您还没有指定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;
这种方法对我有效:
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;
这是一种非常直接的方法,将所有白色像素设置为透明
PS:
Image321
是一个TImage32
组件,包含我的TBitmap32
设置bmp.transparentColor:=clWhite;而不是bmp.outerColor:=clWhite时会发生什么?TBitmap32中没有该组件的成员-我也尝试过从像素中拾取outerColor,但没有效果。bmp.outerColor:=bmp。像素[0,1];谢谢你,我将切换到标准的TImage并尝试修改你的代码。我正在使用Delphi 2010Delphi 2010将很好地处理这个问题,不要忘记TImage还有一些透明选项,如果有用的话,请接受它作为答案。(:这个示例给我一个错误。在“过程TCustomBitmap32.Assign(源代码:TPersistent)”中)在GR32.pas单元;有什么帮助吗?