Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to obtain an images original size and resolution

Posted on 2004-04-19
6
Medium Priority
?
411 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this tutorial viewers will learn how to create blended and gradiated shapes in Illustrator using the blend tool Draw two shapes, one of them in a different color: Select both and create a blend by going to Object > Blend > Make: Blends can also b…
Learn how to set up basic frames and paths in Prezi and understand the open space that Prezi allows you to create presentations in.
Suggested Courses

609 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