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.