First, you can request the first hundreds of bytes of the image using the Range header.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Headers.Set(HttpRequestHeader.UserAgent, "Range: bytes=0-100");
Then you need to decode. The unix file command contains a table of common formats and the location of key information. I would suggest installing Cygwin and taking a look at /usr/share/file/magic .
For gif and png you can easily get image sizes from the first 32 bytes. However, for JPEG files, @Andrew is correct that you cannot reliably obtain this information. You can determine if there is a thumbnail and the size of the thumbnail.
Get the actual jpeg size, you need to scan the start of frame tag. Unfortunately, you cannot reliably determine where this will happen in advance, and a sketch can push it to several thousand bytes.
I would recommend using a range query to get the first 32 bytes. This will allow you to determine the type of file. Then, if it's JPEG, then download the entire file and use the library to get size information.
source share