How to get dimensions of image file in Delphi?

前端 未结 6 1833
南旧
南旧 2021-01-05 00:35

I want to know width and height of an image file before opening that file.

So, how to do that?

EDIT: This refers to jpg, bmp, png and gif types of image fil

6条回答
  •  暖寄归人
    2021-01-05 01:08

    As a complement to Rafael's answer, I believe that this much shorter procedure can detect BMP dimensions:

    function GetBitmapDimensions(const FileName: string; out Width,
      Height: integer): boolean;
    const
      BMP_MAGIC_WORD = ord('M') shl 8 or ord('B');
    var
      f: TFileStream;
      header: TBitmapFileHeader;
      info: TBitmapInfoHeader;
    begin
      result := false;
      f := TFileStream.Create(FileName, fmOpenRead);
      try
        if f.Read(header, sizeof(header)) <> sizeof(header) then Exit;
        if header.bfType <> BMP_MAGIC_WORD then Exit;
        if f.Read(info, sizeof(info)) <> sizeof(info) then Exit;
        Width := info.biWidth;
        Height := abs(info.biHeight);
        result := true;
      finally
        f.Free;
      end;
    end;
    

提交回复
热议问题