Im getting some images from a webpage at a specified url, i want to get their heights and widths. I’m using something like this:
Stream str = null;
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(ImageUrl);
HttpWebResponse wRes = (HttpWebResponse)(wReq).GetResponse();
str = wRes.GetResponseStream();
var imageOrig = System.Drawing.Image.FromStream(str);
int height = imageOrig.Height;
int width = imageOrig.Width;
My main concern with this is that that the image file may actually be very large,
Is there anything I can do? ie specify to only get images if they are less than 1mb?
or is there a better alternative approach to getting the dimension of an image from a webpage?
thanks
Some (all?) image formats include the width and height property in the header of the file. You could just request enough bytes to be able to read the header and then parse them yourself. You can add a range header to your web request that will request only the first 50 bytes (50 is just an example, you’d probably need less) of the image file with:
I suppose this will only work if you know that the formats you are working with include this data.
Edit: Looks like I misunderstood the
AddRangemethod before. It’s fixed now. I also went ahead and tested it out by getting the width and height of a png using this documentation.