为什么加载PNG图像格式图标会导致"Out of system resources"异常?

Cas*_*ady 9 delphi delphi-7

我有一个特定的图标文件,它由PNG压缩图像组成,当我尝试加载它并添加到a时TImageList,Out of system resources会引发异常.

图标文件位于:https://www.dropbox.com/s/toll6jhlwv3cpq0/icon.ico?m

以下是代码,它使用常见类型的图标,但无法使用PNG图像图标:

procedure TForm1.Button1Click(Sender: TObject);
var
  Icon: TIcon;
begin
  try
    Icon := TIcon.Create;
    Icon.LoadFromFile('icon.ico');
    ImageList1.AddIcon(Icon);
    Caption := IntToStr(ImageList1.Count);
  finally
    Icon.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

为什么PNG图像图标格式无法加载Out of system resources异常?如何将这种图标添加到图像列表?

TLa*_*ama 11

问题来源:

在这种情况下,图标是多尺寸图标文件的事实并不重要.图标的位图信息标题以不同的方式在内部读取.您的图标是PNG格式文件图标,没有位图信息标题结构.您获得Out of system resources异常的原因是因为内部使用的过程需要从图标中获取TBitmapInfoHeader结构,然后尝试根据此标头信息创建临时位图.对于你的图标,它是这样读的:

在此输入图像描述

如果你仔细看看标题值,你会计算出系统会尝试创建一个位图,其大小为169478669*218103808像素,每像素21060 B,至少需要有778.5 EB (exabytes)空闲内存:-)

解决方法:

这当然是不可能的(此时:-)并且只是因为PNG文件格式图标没有此位图标题,而是直接包含该位置上的PNG图像.解决这个问题的方法是检查PNG signature图像数据的前8个字节是否有实际检查是否存在PNG图像,如果是,则将其视为PNG图像,否则尝试添加图标通过TIcon对象的常见方式.

在下面的代码中,该ImageListAddIconEx函数迭代图标文件中的所有图标,当存在与图像列表尺寸匹配的图标时,它将被处理.如果在数据偏移位置上存在PNG图像,则处理首先检查那8个字节,如果是,则将该PNG图像添加到图像列表.如果没有,则通过TIcon对象以通用方式添加图标.如果成功,此函数返回图像列表中添加的图标的索引,否则返回-1:

uses
  PNGImage;

type
  TIconDirEntry = packed record
    bWidth: Byte;           // image width, in pixels
    bHeight: Byte;          // image height, in pixels
    bColorCount: Byte;      // number of colors in the image (0 if >= 8bpp)
    bReserved: Byte;        // reserved (must be 0)
    wPlanes: Word;          // color planes
    wBitCount: Word;        // bits per pixel
    dwBytesInRes: DWORD;    // image data size
    dwImageOffset: DWORD;   // image data offset
  end;

  TIconDir = packed record
    idReserved: Word;       // reserved (must be 0)
    idType: Word;           // resource type (1 for icons)
    idCount: Word;          // image count
    idEntries: array[0..255] of TIconDirEntry;
  end;
  PIconDir = ^TIconDir;

function ImageListAddIconEx(AImageList: TCustomImageList;
  AIconStream: TMemoryStream): Integer;
var
  I: Integer;
  Data: PByte;
  Icon: TIcon;
  IconHeader: PIconDir;
  Bitmap: TBitmap;
  PNGImage: TPNGImage;
  PNGStream: TMemoryStream;
const
  PNGSignature: array[0..7] of Byte = ($89, $50, $4E, $47, $0D, $0A, $1A, $0A);
begin
  // initialize result to -1
  Result := -1;
  // point to the icon header
  IconHeader := AIconStream.Memory;
  // iterate all the icons in the icon file
  for I := 0 to IconHeader.idCount - 1 do
  begin
    // if the icon dimensions matches to the image list, then...
    if (IconHeader.idEntries[I].bWidth = AImageList.Width) and
      (IconHeader.idEntries[I].bHeight = AImageList.Height) then
    begin
      // point to the stream beginning
      Data := AIconStream.Memory;
      // point with the Data pointer to the current icon image data
      Inc(Data, IconHeader.idEntries[I].dwImageOffset);
      // check if the first 8 bytes are PNG image signature; if so, then...
      if CompareMem(Data, @PNGSignature[0], 8) then
      begin
        Bitmap := TBitmap.Create;
        try
          PNGImage := TPNGImage.Create;
          try
            PNGStream := TMemoryStream.Create;
            try
              // set the icon stream position to the current icon data offset
              AIconStream.Position := IconHeader.idEntries[I].dwImageOffset;
              // copy the whole PNG image from icon data to a temporary stream
              PNGStream.CopyFrom(AIconStream,
                IconHeader.idEntries[I].dwBytesInRes);
              // reset the temporary stream position to the beginning
              PNGStream.Position := 0;
              // load the temporary stream data to a temporary TPNGImage object
              PNGImage.LoadFromStream(PNGStream);
            finally
              PNGStream.Free;
            end;
            // assign temporary TPNGImage object to a temporary TBitmap object
            Bitmap.Assign(PNGImage);
          finally
            PNGImage.Free;
          end;
          // to properly add the bitmap to the image list set the AlphaFormat
          // to afIgnored, see e.g. http://stackoverflow.com/a/4618630/960757
          // if you don't have TBitmap.AlphaFormat property available, simply
          // comment out the following line
          Bitmap.AlphaFormat := afIgnored;
          // and finally add the temporary TBitmap object to the image list
          Result := AImageList.Add(Bitmap, nil);
        finally
          Bitmap.Free;
        end;
      end
      // the icon is not PNG type icon, so load it to a TIcon object
      else
      begin
        // reset the position of the input stream
        AIconStream.Position := 0;
        // load the icon and add it to the image list in a common way
        Icon := TIcon.Create;
        try
          Icon.LoadFromStream(AIconStream);
          Result := AImageList.AddIcon(Icon);
        finally
          Icon.Free;
        end;
      end;
      // break the loop to exit the function
      Break;
    end;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

用法:

procedure TForm1.Button1Click(Sender: TObject);
var
  Index: Integer;
  Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  try
    Stream.LoadFromFile('d:\Icon.ico');
    Index := ImageListAddIconEx(ImageList1, Stream);
    if (Index <> -1) then
      ImageList1.Draw(Canvas, 8, 8, Index);
  finally
    Stream.Free;
  end;
end;
Run Code Online (Sandbox Code Playgroud)

结论:

我想说如果微软推荐使用PNG图标格式(从Windows Vista开始支持),更新ReadIcon程序就Graphics.pas可以将其考虑在内了.

要阅读的内容: