delphi - 从 TImageList 保存透明(alpha channel )PNG

标签 delphi png c++builder imagelist timagelist

我有一个 TImageList,其中包含透明图标(32 位,带 Alpha channel )。我想要做的是将基于图像索引的各个图标保存为 PNG 文件,同时保留 Alpha channel 透明度。使用 RAD Studio 2010,因此它具有 TPngImage 支持,无需第三方库。使用此处的方法将图像从 PNG“ Sprite ”图像加载到 TImageList - Add a png image to a imagelist in runtime using Delphi XE - 因此加载时保留透明度。现在我需要将它们单独保存出来,换句话说,从已经加载到 TImageList 中的 Sprite 图像中提取单个图像。

到目前为止我的代码:

int imageindex = 123;
boost::scoped_ptr<TPngImage>         png(new TPngImage);
boost::scoped_ptr<Graphics::TBitmap> bmp(new Graphics::TBitmap);

MyImageList->GetBitmap(imageindex, bmp.get()); // Using GetBitmap to copy TImageList image into separate TBitmap

png->Assign(bmp.get()); // Assign that bitmap to TPngImage
png->SaveToFile("C:\\filename.png");

上面的方法有效,但它以白色背景保存(保存后不保留透明度)。我可能错过了一个简单的步骤,但无法弄清楚。

Delphi 代码也很受欢迎,应该不难翻译。

最佳答案

是的,您可以从添加 PNG 图像的 TImageList 中获取该图像。下面的代码允许您执行此操作!
首先,将 PngImage 添加到 uses 子句中。

procedure LoadPNGFromImageList(AImageList: TCustomImageList; AIndex: Integer; var ADestPNG: TPngImage);
const
  PixelsQuad = MaxInt div SizeOf(TRGBQuad) - 1;
type
  TRGBAArray = Array [0..PixelsQuad - 1] of TRGBQuad;
  PRGBAArray = ^TRGBAArray;
var
  ContentBmp: TBitmap;
  RowInOut: PRGBAArray;
  RowAlpha: PByteArray;
  X: Integer;
  Y: Integer;
begin
  if not Assigned(AImageList) or (AIndex < 0) or
     (AIndex > AImageList.Count - 1) or not Assigned(ADestPNG)
  then
    Exit;

  ContentBmp := TBitmap.Create;
  try
    ContentBmp.SetSize(ADestPNG.Width, ADestPNG.Height);
    ContentBmp.PixelFormat := pf32bit;

    // Allocate zero alpha-channel
    for Y:=0 to ContentBmp.Height - 1 do
      begin
        RowInOut := ContentBmp.ScanLine[Y];
        for X:=0 to ContentBmp.Width - 1 do
          RowInOut[X].rgbReserved := 0;
      end;
    ContentBmp.AlphaFormat := afDefined;

    // Copy image
    AImageList.Draw(ContentBmp.Canvas, 0, 0, AIndex, true);

    // Now ContentBmp has premultiplied alpha value, but it will
    // make bitmap too dark after converting it to PNG. Setting
    // AlphaFormat property to afIgnored helps to unpremultiply
    // alpha value of each pixel in bitmap.
    ContentBmp.AlphaFormat := afIgnored;

    // Copy graphical data and alpha-channel values
    ADestPNG.Assign(ContentBmp);
    ADestPNG.CreateAlpha;
    for Y:=0 to ContentBmp.Height - 1 do
      begin
        RowInOut := ContentBmp.ScanLine[Y];
        RowAlpha := ADestPNG.AlphaScanline[Y];
        for X:=0 to ContentBmp.Width - 1 do
          RowAlpha[X] := RowInOut[X].rgbReserved;
      end;
  finally
    ContentBmp.Free;
  end;
end;

看图。它描述了如果我们设置或不设置这样的代码行将会发生什么:

ContentBmp.AlphaFormat := afIgnored;

enter image description here
图一是设置afIgnored的结果,第二张图是设置afIgnored的结果,允许使用之前设置的 af定义

原始图像是名为图 1 的图像

在应用程序中使用上面的代码:

procedure TForm1.aButton1Click(Sender: TObject);
var
  DestPNG: TPngImage;
begin
  DestPNG := TPNGImage.Create;
  try
    // Initialize PNG
    DestPNG.CreateBlank(COLOR_RGBALPHA, 8, 60, 60);

    // Obtain PNG from image list
    LoadPNGFromImageList(ImageList1, 0, DestPNG);

    // Output PNG onto Canvas
    DestPNG.Draw(Canvas, Rect(0, 0, 60, 60));
    DestPNG.SaveToFile('C:\MyPNGIcon.png');
  finally
    DestPNG.Free;
  end;
end;  

关于delphi - 从 TImageList 保存透明(alpha channel )PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52794810/

相关文章:

python - 如何在 Excel XLS 文件中提取 PNG 附件?

android - 如何删除 android 启动器图标的黑色背景?

c++ - 在 TreeView C++ BUILDER 中突出显示项目

Delphi XE2 DataSnap - 通过 HTTPS 使用 REST 时为 'Connection Closed Gracefully'

delphi - 如何将 vcl 样式应用到 TPopupmenu?

php - 为什么我的透明 div 上的链接在 IE 中变得不可点击?

java - 在 C++ Builder 中将 Delphi 接口(interface)转换为 TObject* 并调用正确的 ClassName() 函数

c++ - 在 C++Builder 中显示 #include 层次结构

delphi - 我如何知道字体是否具有给定 Unicode 字符的字形?

delphi - FMX : dropping selfmade component on form duplicates subcomponents