Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get dimensions of an image file in Delphi?

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

So, how can I do that?

This refers to JPEG, BMP, PNG and GIF types of image files.

like image 985
Srdjan Vukmirica Avatar asked Mar 04 '13 19:03

Srdjan Vukmirica


1 Answers

If by 'image file' you mean those raster image files recognised by the VCL's graphics system, and by 'before opening' you mean 'before the user is likely to notice that the file is opened', then you can do this very easily:

var
  pict: TPicture;
begin
  with TOpenDialog.Create(nil) do
    try
      if Execute then
      begin
        pict := TPicture.Create;          
        try
          pict.LoadFromFile(FileName);
          Caption := Format('%d×%d', [pict.Width, pict.Height])
        finally
          pict.Free;
        end;
      end;
    finally
      Free;
    end;

Of course, the file is opened, and this requires a lot of memory if the image is big. However, if you need to obtain metatada (like dimensions) without loading the file, I believe you need a more 'complicated' solution.

like image 157
Andreas Rejbrand Avatar answered Oct 20 '22 01:10

Andreas Rejbrand