How to get dimensions of image file in Delphi?

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

    If anyone yet interested in retrieving TIFF image dimensions without loading the graphic, there is a proven method that works perfectly for me in all environments. I also found another solution for that, but it returned wrong values from Illustrator-generated TIFFs. But there is a fantastic graphic library, called GraphicEx by Mike Lischke (TVirtualStringTree's very talented developer). There are implementations of many popular image formats and all of them descend from the base class TGraphicExGraphic, that implements ReadImageProperties virtual method. It is stream-based and only reads the fileheader in all implementations. So it is lightning-fast... :-)

    So, here is a sample code, that retrieves a TIFF's dimensions (the method is the same for all graphic implementation, PNG,PCD,TGA,GIF,PCX,etc):

    Uses ..., GraphicEx,...,...;
    
    Procedure ReadTifSize (FN:String; Var iWidth,iHeight:Integer);
    Var FS:TFileStream;
        TIFF:TTIFFGraphic;
    Begin
      iWidth:=0;iHeight:=0;
      TIFF:=TTIFFGraphic.Create;
      FS:=TFileStream.Create(FN,OF_READ);
    
      Try
        TIFF.ReadImageProperties(FS,0);
        iWidth:=TIFF.ImageProperties.Width;
        iHeight:=TIFF.ImageProperties.Height;
      Finally
        TIFF.Destroy;
        FS.Free;
      End;
    End;
    

    That's all... :-) And this is the same for all the graphic implementations in the unit.

提交回复
热议问题