Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. </p> <p>Here is an out of the box method for you :)</p> <pre><code>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&lt;&lt;=8; ushort lo=rdr.ReadByte(); return (ushort)(hi|lo); } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload