如何保存具有透明度的png文件? [英] How to save a png file with transparency?
问题描述
我正在使用Barcode Studio 2011将QR码绘制到Graphics32 - TImage32组件中,并且我想以png格式保存,但是我在Graphics32的OuterColor中指定了白色透明。
OnFormCreate我刚刚
procedure TForm1.FormCreate(Sender:TObject);
begin
psBarcodeComponent1.BarCode:='some text here ...';
结束
此刻,我将画作分配给按钮点击事件
procedure TForm1.Button8Click(Sender:TObject); //绘制条形码
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;
//使Coord的图像大小
Coords:= Rect(0,0,image.Width,image.Height);
psBarcodeComponent1.PaintBarCode(bmp.Canvas,Coords);
image.Bitmap.Assign(bmp);
结束
我正在使用Vampyre Imaging Library将Bitmap转换为PNG格式,但我很乐意使用任何库,功能和建议 - 我一直在试图做这个近一个星期!我已阅读并重读了graphics32的文档以及Vampyre Imaging Library,但是我没有尝试将白色转换为透明的颜色。我已经尝试了clWhite,clWhite32,并将drawMode设置为dmBlend,并将ChromaKey功能全部应用到无效,但很沮丧,咖啡和一点啤酒;)
是我如何保存它...
procedure TForm1.Button7Click(Sender:TObject); //保存与Vampyre Imaging Lib
{尝试以透明度保存为PNG格式}
var
FImage:TSingleImage;
begin
FImage:= TSingleImage.Create;
ConvertBitmap32ToImage(image.Bitmap,FImage);
FImage.SaveToFile('VampyreLibIMG.png');
结束
这将导致黑色的缩略图,并在Windows Photo Viewer中查看时完全透明。 p>
我希望我提供足够的信息,有人能够帮助我。
Chris
此方法适用于我:
使用GR32,GR32_PNG,GR32_PortableNetworkGraphic;
var
Y:整数;
X:整数;
Png:TPortableNetworkGraphic32;
函数IsWhite(Color32:TColor32):Boolean;
begin
结果:=(TColor32Entry(Color32).B = 255)和
(TColor32Entry(Color32).G = 255)和
(TColor32Entry(Color32) 255);
结束
begin
with Image321 do
begin
Bitmap.ResetAlpha;
为Y:= 0到Bitmap.Height-1 do
for X:= 0 to Bitmap.Width-1 do
begin
if IsWhite(Bitmap.Pixel [X, Y]),然后
Bitmap.Pixel [X,Y]:= Color32(255,255,255,0);
结束
Png:= TPortableNetworkGraphic32.Create;
Png.Assign(Bitmap);
Png.SaveToFile('C:\Temp\NowTransparent.png');
Png.Free;
结束
结束
这使用 GR32 PNG库。这是一个非常直接的方式,将所有白色像素设置为透明。
PS: Image321
是一个 TImage32
组件,包含我的 TBitmap32
。
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.
OnFormCreate I have just
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! 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. 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 ;)
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.
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. It's a pretty direct way, setting all white pixels to transparent.
PS: Image321
is a TImage32
component, containing my TBitmap32
.
这篇关于如何保存具有透明度的png文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!