Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reliably get an image dimensions in .NET without loading the image?

Tags:

.net

image

I know how to get the size (x,y) of an image

Image.FromFile("cat.jpg").Size

BUT that requires loading the image from memory.

When I view the images in Windows Explorer it shows me the size.

  • How do I access that size?
  • Is it reliable for all images? Does Windows explorer need to have 'seen' the image first for that size to be available. I don't want to upload my images to a server and have no metadata available if I never opened the folder.
like image 952
Simon_Weaver Avatar asked Feb 16 '09 07:02

Simon_Weaver


People also ask

Can we specify the width and height of an image in a Web page?

The height and width of an image can be set using height and width attribute. The height and width can be set in terms of pixels. The <img> height attribute is used to set the height of the image in pixels. The <img> width attribute is used to set the width of the image in pixels.

What will happen if width and height of the image is not specified?

If width and height are not specified, the page will flicker while the image loads.


4 Answers

You can just use Image.FromStream(Stream, bool, bool) with "false" 2nd and 3rd parameter to avoid loading file into memory.

like image 175
QrystaL Avatar answered Sep 28 '22 05:09

QrystaL


If you want to get the image dimensions (width and hight) with out loading the image, you need to read some part of jpeg file yourself as below.

Here is an out of the box method for you :)

public static Size GetJpegImageSize(string filename) {     FileStream stream=null;     BinaryReader rdr=null;     try {         stream=File.OpenRead(filename);         rdr=new BinaryReader(stream);         // keep reading packets until we find one that contains Size info         for(; ; ) {             byte code=rdr.ReadByte();             if(code!=0xFF) throw new ApplicationException(                     "Unexpected value in file "+filename);             code=rdr.ReadByte();             switch(code) {                 // filler byte                 case 0xFF:                     stream.Position--;                     break;                 // packets without data                 case 0xD0: case 0xD1: case 0xD2: case 0xD3: case 0xD4:                  case 0xD5: case 0xD6: case 0xD7: case 0xD8: case 0xD9:                     break;                 // packets with size information                 case 0xC0: case 0xC1: case 0xC2: case 0xC3:                 case 0xC4: case 0xC5: case 0xC6: case 0xC7:                 case 0xC8: case 0xC9: case 0xCA: case 0xCB:                 case 0xCC: case 0xCD: case 0xCE: case 0xCF:                     ReadBEUshort(rdr);                     rdr.ReadByte();                     ushort h=ReadBEUshort(rdr);                     ushort w=ReadBEUshort(rdr);                     return new Size(w, h);                 // irrelevant variable-length packets                 default:                     int len=ReadBEUshort(rdr);                     stream.Position+=len-2;                     break;             }         }     } finally {         if(rdr!=null) rdr.Close();         if(stream!=null) stream.Close();     } }  private static ushort ReadBEUshort(BinaryReader rdr) {     ushort hi=rdr.ReadByte();     hi<<=8;     ushort lo=rdr.ReadByte();     return (ushort)(hi|lo); } 

This is not my code, got this example some time back in code project. I copied and and saved it to my utility snippets, don't remember the link though.

like image 29
amazedsaint Avatar answered Sep 28 '22 05:09

amazedsaint


The article Reading Image Headers to Get Width and Height provides several solution possibilities:

  • Multi-threading
  • Reading file headers, reverting to full image load if header is lacking
  • Creating your own index for image attributes
like image 41
rpisryan Avatar answered Sep 28 '22 05:09

rpisryan


Doing this usually involves actually opening the file and reading only its header (the first few bytes) to find its format and the size. You don't need to read the whole file to find out it's size. Sometimes the size is even in a specific offset in the file so finding the size is only reading 8 bytes.

You can use filemon.exe to actually find out what windows explorer itself does to find the size.

like image 26
shoosh Avatar answered Sep 28 '22 04:09

shoosh