Some (all?) Image formats include the width and height property in the file header. You can simply request enough bytes to read the header and then parse them yourself. You can add a range header to your web request that will only request the first 50 bytes (50 is just an example, which you probably need less) image file with:
wReq.AddRange(0, 50);
I assume that this will work only if you know that the formats you work with include this data.
Edit: Looks like I didn't understand the AddRange method AddRange . This is now fixed. I also went ahead and checked it, getting the width and height of the png using this documentation .
static void Main(string[] args) { string imageUrl = "http://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png"; byte[] pngSignature = new byte[] { 137, 80, 78, 71, 13, 10, 26, 10 }; HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(imageUrl); wReq.AddRange(0, 30); WebResponse wRes = wReq.GetResponse(); byte[] buffer = new byte[30]; int width = 0; int height = 0; using (Stream stream = wRes.GetResponseStream()) { stream.Read(buffer, 0, 30); }
source share