0

I have an image's URL. Is it possible to find out its size(in bytes) and dimensions without downloading the complete image?

EDIT

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "HEAD";
HttpWebResponse resp = (HttpWebResponse)(req.GetResponse());
System.Console.WriteLine(resp.ContentLength);

I have written this code. It works fine for first two time and the third time it gives WebException i.e. "operation timed out" irrespective of the image url. Is there something that I am missing here?

Ravi Gupta
  • 6,258
  • 17
  • 56
  • 79

2 Answers2

4

No, this is not possible.

You can get its size in bytes by issuing an HTTP HEAD command (instead of a GET); this will return the HTTP headers only, omitting the contents.

The HTTP header of an image will return its size in bytes:

Content-length: 6372
Content-type: image/jpeg

but not its dimensions.

So you'll have to do an HTTP GET...

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • ok ... i have written a piece of code and when I try to run it against 3 images, first 2 always works fine but the 3rd one is giving WebException i.e operation timed out but I am able to open it in the browser. Any reason for such behavior? – Ravi Gupta Oct 23 '12 at 08:32
  • @RaviGupta: That is unanswerable without looking at your code. Very probably you need to work on that code to get it to work properly. – Roy Dictus Oct 30 '12 at 13:50
0

Basically, yes. If you know what kind of image it is e.g. png or jpg (you can also get this information from the MIME-type of your HTTP-connection, you can download the header only and read the image extents from there. Also see this related question:

What is the header size of png, jpg/jpeg, bmp, gif and other common graphics format?

Community
  • 1
  • 1
msteiger
  • 2,024
  • 16
  • 22