【问题标题】:How to save a png file with transparency?如何保存具有透明度的png文件?
【发布时间】:2011-10-20 09:59:47
【问题描述】:

我正在使用 Barcode Studio 2011 将 QR 码绘制到 Graphics32 - TImage32 组件中,我想将其保存为 png 格式,但白色是透明的,这是我在 Graphics32 的 OuterColor 中指定的。

OnFormCreate 我刚刚

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 Imaging Library 将位图转换为 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 照片查看器中查看时它是完全透明的。

我希望我提供了足够的信息并且有人能够帮助我。

克里斯

【问题讨论】:

  • 设置 bmp.transparentColor := clWhite; 时会发生什么而不是 bmp.outerColor := clWhite?
  • TBitmap32 中没有该成员 - 我也尝试从像素中选择 OuterColor 但它没有效果。 bmp.OuterColor := bmp.Pixel[0,1];

标签: image delphi png save transparency


【解决方案1】:

这种方法对我有用:

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;

这使用GR32 PNG library。这是一种非常直接的方法,将所有白色像素设置为透明。

PS:Image321 是一个TImage32 组件,包含我的TBitmap32

【讨论】:

  • 这个例子给了我一个错误。在单元 GR32.pas 中的“过程 TCustomBitmap32.Assign(Source: TPersistent)”中;有什么帮助吗?
【解决方案2】:

您还没有指定 Delphi 版本,但是如果您的 delphi 版本有“PngImage”(我相信它带有 D2009+),下面的代码可以完美运行(在 Gimp 和 Windows 照片查看器中加载,它会绘制一个框架和一些文本有透明背景,随意玩吧:

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;

【讨论】:

  • 谢谢您,我将切换到标准 TImage 并尝试调整您的代码。我正在使用 Delphi 2010
  • Delphi 2010 将完美地处理这个问题,不要忘记 TImage 也有一些透明选项,如果有帮助,请接受它作为答案。 (:
猜你喜欢
  • 2013-04-15
  • 2012-05-29
  • 2023-03-31
  • 2012-03-29
  • 2018-10-19
  • 2013-04-11
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多