.ico 文件的 TPicture 宽度和高度报告不正确 (Delphi 7)

TPicture Width and Height reported incorrectly for .ico files (Delphi 7)

使用 Delphi 7. 我有一个简单的例程成功加载 .bmp.emf.wmf.ico.jpg 文件(下面给出的代码)。我的问题是每个 .ico(图标)文件总是将 TImage.TPicture.WidthTImage.TPicture.Height 报告为“32”。所有图标都是 32 位的,里面只有一页。实际尺寸是多少并不重要(我试过 16x16、32x32、64x64 和 128x128)。

如果我手动将 TImage.WidthTImage.Width 设置为我 知道的 图标大小,则图像显示效果很好。所有其他文件类型都正确报告大小。

为什么 .ico 文件有问题,我该如何更正或解决该问题。

procedure TfrmImageLoader.btnBrowseClick(Sender: TObject);
var
    openPictureDlg: TOpenPictureDialog;
    jpgImage: TJPEGImage;
    testWidth, testHeight: Integer;
begin
    // Browse for the image file
    openPictureDlg := TOpenPictureDialog.Create(Self);
    if (openPictureDlg.Execute) then
        begin
        // Check if file exists
        if (FileExists(openPictureDlg.FileName)) then
            begin
            // Load the image into out image component
            imgLoaded.Visible := False;
            if (IsJPEG(openPictureDlg.FileName)) then
                begin
                jpgImage := TJPEGImage.Create();
                jpgImage.LoadFromFile(openPictureDlg.FileName);
                imgLoaded.Picture.Assign(jpgImage);
                jpgImage.Free();
                end
            else
                begin
                imgLoaded.Picture.LoadFromFile(openPictureDlg.FileName);
                end;
                
            // Test width...here's the problem. Icons always report "32".
            testWidth := m_imgLoaded.Picture.Width;
            testHeight := m_imgLoaded.Picture.Height;
            m_imgLoaded.Visible := True;
            end
        else
            begin
            // File does not exist
            MessageDlg('File does not exist', mtWarning, [mbOK], 0);
            end;
        end;

    // Clean up
    openPictureDlg.Free();
end;

更新 1

作为测试,我将文件加载为 TIcon,但结果是一样的。

ico: TIcon;
// ...
ico := TIcon.Create();
ico.LoadFromFile(openPictureDlg.FileName);
testWidth := ico.Width;   // Still 32, regardless of the actual size
testHeight := ico.Height;
ico.Free();

更新 2

查看已接受的答案。基本上有两种方法可以获得正确的大小(a)加载图标,分配给 TBitmap,然后读取位图大小或(b)读取图标 header,字节 7 和 8 是 width/height.后者在我的测试中快了约 20 倍,代码如下:

procedure GetTrueIconSize2(const cszIcon: String; var trueW: Integer; var trueH: Integer);
var
    fs: TFileStream;
    firstBytes: AnsiString;
begin
    // The size of image/vnd.microsoft.icon MIME files (Windows icon) is in the header
    // at bytes 7 & 8. A value of "0" means "256" (the largest icon size supported).
    fs := TFileStream.Create(cszIcon, fmOpenRead);
    try
        SetLength(firstBytes, 8);
        fs.Read(firstBytes[1], 8);
        trueW := Integer(firstBytes[7]);
        if (trueW = 0) then
            trueW := 256;

        trueH := Integer(firstBytes[8]);
        if (trueH  = 0) then
            trueH := 256;
    finally
        fs.Free();
    end;
end;

解决方法是自己解析 ICO 文件,这很简单:https://en.wikipedia.org/wiki/ICO_(file_format) - 这样您就可以轻松了解每个条目的尺寸。在最简单的情况下(只有一张图片)文件的前 6 个字节必须是 #0#0#1#0#1#0 并且字节 7 和 8 是宽度和高度。