How to get dimensions of image file in Delphi?

前端 未结 6 1835
南旧
南旧 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:13

    Since GetGIFSize in Rafael answer is broken and utterly complicated here is my personal version of it:

    function GetGifSize(var Stream: TMemoryStream; var Width: Word; var Height: Word): Boolean;
    var
        HeaderStr: AnsiString;
    
    begin
        Result := False;
        Width := 0;
        Height := 0;
    
        //GIF header is 13 bytes in length
        if Stream.Size > 13 then
        begin
            SetString(HeaderStr, PAnsiChar(Stream.Memory), 6);
            if (HeaderStr = 'GIF89a') or (HeaderStr = 'GIF87a') then
            begin
                Stream.Seek(6, soFromBeginning);
                Stream.Read(Width, 2);  //Width is located at bytes 7-8
                Stream.Read(Height, 2); //Height is located at bytes 9-10
    
                Result := True;
            end;
        end;
    end;
    

    Found it by reading the RFC.

提交回复
热议问题