4

私の仕事は:

  1. TBitmapオブジェクトを作成します。
  2. 透明色で塗りつぶします(アルファ= 0)。
  3. このビットマップをTPngImageに割り当てます。
  4. アルファ透明度でPNGファイルを保存します。

Delphi XEでそれを行うにはどうすればよいですか?

var
  Png: TPngImage;
  X, Y: Integer;
  Bitmap: TBitmap;
begin
  Bitmap := TBitmap.Create();
  Bitmap.PixelFormat := pf32bit;
  Png := TPngImage.Create();
  try
    Bitmap.SetSize(100, 100);

    // How to clear background in transparent color correctly?
    // I tried to use this, but the image in PNG file has solid white background:
    for Y := 0 to Bitmap.Height - 1 do
      for X := 0 to Bitmap.Width - 1 do
        Bitmap.Canvas.Pixels[X, Y]:= $00FFFFFF;

    // Now drawing something on a Bitmap.Canvas...
    Bitmap.Canvas.Pen.Color := clRed;
    Bitmap.Canvas.Rectangle(20, 20, 60, 60);

    // Is this correct?
    Png.Assign(Bitmap);
    Png.SaveToFile('image.png');
  finally
    Png.Free();
    Bitmap.Free();
  end;
end;
4

1 に答える 1

5

多かれ少なかれドリンの答えのコピー。透明なpng画像の作成方法と背景をクリアする方法を示しています。

uses
  PngImage;
...

var
  bmp: TBitmap;
  png: TPngImage;
begin
  bmp := TBitmap.Create;
  bmp.SetSize(200,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;
于 2012-06-13T11:11:49.110 に答える