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

标签 image delphi icons 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)读取图标标题,字节 7 和 8 是宽度/高度。在我的测试中后者快了约 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 字节是宽度和高度。

关于image - .ico 文件的 TPicture 宽度和高度报告不正确 (Delphi 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62970779/

相关文章:

python - 将图像中的白色像素更改为不同的颜色

javascript - 如何在 VR 耳机中查看来自网站的 VR/360 照片?

image - OpenCV 2.4.5读取并显示图像崩溃

delphi - ADOQuery.Filter 不适用于 LIKE

arrays - 将一行文本放入由引号分隔的数组中?德尔福

html - 图标字体链接图标在 iphone 上无法点击

html - 初学者在 Bootstrap 中与平板电脑大小的图像网格作斗争

android - 在 Delphi XE5 for Android 中拦截接收到的短信

java - 增加 Eclipse 中菜单/工具栏图标的大小

html - 我可以更改 Font Awesome 齿轮图标的颜色吗?