How to save a png file with transparency?

I am using Barcode Studio 2011 to draw a QR code in the Graphics32-TImage32 component, and I want to save it in png format, but with the transparent white color that I specified in OuterColor Graphics32.

OnFormCreate I only have

procedure TForm1.FormCreate(Sender: TObject);
begin
  psBarcodeComponent1.BarCode := 'some text here...';
end;

and at the moment I have a picture assigned to the Click Button 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;

Vampyre Imaging Library Bitmap 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 Photo Viewer .

, - .

+5
2

:

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. , .

PS: Image321 - TImage32, my TBitmap32.

+4

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;
+3

All Articles