?
Solved

How to obtain an images original size and resolution

Posted on 2004-04-19
6
Medium Priority
?
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Learn how to use the remote presentation tool in Prezi to allow you to harness the power of this cloud based presentation platform. You can show your presentation with a remote audience using this free tool.
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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