Solved

How to obtain an images original size and resolution

Posted on 2004-04-19
6
398 Views
Last Modified: 2013-11-19
Hello!

I have searched for info on obtaining the original picture resolution of a bmp, jpg, tif or whatever type image file and I have not been successful. I must be missing something!

What I would like to do is load a piture into a TImage (or similar) and obtain that picture's original dpi setting. From there I can get what I really want is the picture's original size in inches. Some pictures may be scanned at 300 dpi or a digital photo taken at 640 X 480 but have a resolution of 96 dpi or 150 dpi or whatever the user selected to scan or get the picture.

You know...

some apps can tell you that an image's original size is 5 inches X 7 inches with a dpi of 100 dpi, etc. Like MS Paint.
How can I do this?

Thanks for your help!
0
Comment
Question by:Johnjces
  • 2
  • 2
6 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10861017

   the information should be in the header. Take a look at this - for example JPG ...

   http://www.fastgraph.com/help/jpeg_header_format.html
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10861057

   The bitmap contains it :

   http://astronomy.swin.edu.au/~pbourke/dataformats/bmp/

   You have information for Pixels per meter
0
 
LVL 18

Author Comment

by:Johnjces
ID: 10861275
Thank-you! This what I have been seeking. I thought that such may be kept there but I am TERRIBLE when it comes to file manipulation and in obtaining this data.

Might you be so kind as to supply some smaple code on retrieving this info from a bmp and jpg? AGain, my coding skills in direct file manipulation and pasrsing is terrible.
0
 

Expert Comment

by:lopardo2003
ID: 10865039
0
 
LVL 18

Accepted Solution

by:
Johnjces earned 0 total points
ID: 10872591
OK... Here is the answer for all to see. Found this from other sources.

Thank-you too to lopardo2003 and Ivanov_G for their help.

Source follows:

Posted on Earl F. Glynn's site:

...a bitmap doesn't really have a dpi as far as the screen
is concerned, just a height and width. But the original dpi
is indispensable for determining the original size or the
scale of the original scan.

IMPORTANT -> Most bitmaps DO NOT have any data in this portion
of the header for whatever reason. So, if Nil or 0 or empty,
make sure you have a routine to take care of such problem!

function GetBMPFileDPI(FileName: String): LongInt;
var
 Stream: TFileStream;
 Data: Word;
 A: Double;
begin
 try
   Result := 0;
   Stream := TFileStream.Create(FileName,
                    fmOpenRead or fmShareDenyWrite);
   Stream.Position := 38;
   if Stream.Read(Data,2) = 2 then
   begin
     A := Data;
     Result := Round(A / 39.370079);
   end;
 finally
   Stream.Free;
 end;
end;


Now, because all graphics file formats differ. GIF is one
format that does not store a value. It's supposed to always
be 96 ppi.

Here is how to get it from a JPEG:


Interrogate the file header.

Byte 13 gives the dimensions in which the pixel density is given later
in the header, 0 = pixels, 1 = DPI, 2 = dots per centimeter.

Bytes 14/15 give X density.
Bytes 16/17 give Y density.



procedure TForm1.ButtonJPGClick(Sender: TObject);
  CONST
    BufferSize = 50;
  VAR
    Buffer     :  STRING;
    index      :  INTEGER;
    FileStream :  TFileStream;
    xResolution:  WORD;
    yResolution:  WORD;
begin
  IF   OpenDialog.Execute
  THEN BEGIN
    FileStream := TFileStream.Create(OpenDialog.FileName,
                                     fmOpenRead OR fmShareDenyNone);
    TRY
      SetLength(Buffer, BufferSize);
      FileStream.Read(buffer[1], BufferSize);
      index := POS('JFIF'+#$00, buffer);
      IF   index > 0
      THEN BEGIN
        FileStream.Seek(index+7, soFromBeginning);
        FileStream.Read(xResolution, 2);
        FileStream.Read(yResolution, 2);

        xResolution :=  Swap(xResolution);
        yResolution :=  Swap(yResolution);

        ShowMessage('xResolution=' +
                    IntToStr(xResolution) +
                    ', yResolution=' +
                    IntToStr(yResolution))
      END
    FINALLY
      FileStream.Free
    END
  END
end;
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Many programs have tried to outwit PowerPoint in terms of technology and skill. These programs, however, still lack several characteristics that PowerPoint has possessed from the start. Here's why PowerPoint replacements won't entirely work for desi…
In this second Micro Tutorial in the series,  viewers will learn more basic shortcuts and functions of Illustrator. In this tutorial you will learn about the eyedropper tool, zoom , ellipses and more.
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now