Solved

How to obtain an images original size and resolution

Posted on 2004-04-19
6
401 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is anyone willing to have a look through this code and help debug? 25 83
tidtcpserver connection lost handle 2 102
Create a path if not exists 7 89
Performance of SQL statement 37 127
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Add visuals to your Prezi to keep your audience engaged. Learn how to embed images, edit them, and more in this video micro tutorial. Select "Insert" from the top menu in your Prezi editor: Select "Image": A toolbar will pop in from the right …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

840 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