How do I get the dimensions of a graphic file?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have two jpegs -- one portrait, one landscape. I need the ability to tell
the length and height of the graphic for further processing. How can I do
this using CSharp?

thank you
 
CSharpie said:
I have two jpegs -- one portrait, one landscape. I need the ability to
tell
the length and height of the graphic for further processing. How can I do
this using CSharp?

thank you

You can use the FromFile (or FromStream) Method of the System.Drawing.Image
Class to create an Image instance and then look at the Width and Height
properties.

Christof
 
Hi,

CSharpie said:
I have two jpegs -- one portrait, one landscape. I need the ability to
tell
the length and height of the graphic for further processing. How can I do
this using CSharp?

thank you

In addition to the other comments

Check the jpeg documentation, I'm pretty sure that the dimensions are kept
somewhere in teh file, if so you do not need to read the entire file in
memory.
 
Kevin, your answer was right on. I wish I knew the .net libraries as well as
you.

thanks
 
Hi,





In addition to the other comments

Check the jpeg documentation, I'm pretty sure that the dimensions are kept
somewhere in teh file, if so you do not need to read the entire file in
memory.

Agreed. Many graphics packages play this trick.

However, I don't know of any way to do that using pure .NET, without
resorting to binary reads of the file, etc.

I have the same problem with TIF files: I want just the dimensions,
but have to read the entire file into an Image object in order to get
them. It would be nice to have a method that would get me the
dimensions in the quickest, most efficient way possible.
 
Hi,

Bruce Wood said:
Agreed. Many graphics packages play this trick.

However, I don't know of any way to do that using pure .NET, without
resorting to binary reads of the file, etc.

Well, if you implement such a method and package it in a class, then to the
calling code it will be a pure .net solution :)
But yes, you will have to open the binary file, read the header and get that
info from there (if possible)

I have the same problem with TIF files: I want just the dimensions,
but have to read the entire file into an Image object in order to get
them. It would be nice to have a method that would get me the
dimensions in the quickest, most efficient way possible.


did you check the format definition?
Maybe you can just read the header and get that info from there.
 
Agreed. Many graphics packages play this trick.
However, I don't know of any way to do that using pure .NET, without
resorting to binary reads of the file, etc.

It's certainly possible to read this information from the tag structure of a JPEG file without parsing and decompressing
the entire image but as you say you will need to use a binary reader to get at it. There's a class on my site that does
just this but it's written in VB6 - May be useful as a starting point though.
I have the same problem with TIF files: I want just the dimensions,
but have to read the entire file into an Image object in order to get
them. It would be nice to have a method that would get me the
dimensions in the quickest, most efficient way possible.

TIFF images are also pretty easy to get document size information out of, avoiding for the moment multi-page TIFFs,
which are slightly more complex but still reasonably easily parsable. The only potentially nasty there is that the
format can be written either little of big endian, so you need to wrap calls to the binary stream to potentially flip
the data if the byte order is reversed.
Have a look over on www.wotsit.org for the format specification, you just need to parse the IFD structure at the start
of the file so you can ignore the vast majority of the document.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top