5

I want to check size image from url in C#.

Ex: Url: http://img.khoahoc.tv/photos/image/2015/05/14/hang_13.jpg

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Nam Le
  • 53
  • 1
  • 4
  • Refer to http://stackoverflow.com/questions/12079794/get-size-of-image-file-before-downloading-from-web – GSP Jan 03 '17 at 04:04

3 Answers3

11

Download and check:

string image = @"http://img.khoahoc.tv/photos/image/2015/05/14/hang_13.jpg";
byte[] imageData = new WebClient().DownloadData(image);
MemoryStream imgStream = new MemoryStream(imageData);
Image img = Image.FromStream(imgStream);

int wSize = img.Width;
int hSize = img.Height;
Backs
  • 24,430
  • 5
  • 58
  • 85
0
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(@"http://img.khoahoc.tv/photos/image/2015/05/14/hang_13.jpg";);

HttpWebResponse response = (HttpWebResponse)req.GetResponse();

Stream stream = response.GetResponseStream();

Image img = Image.FromStream(stream);

stream.Close();

MessageBox.Show("Height: " + img.Height + " Width: " + img.Width);
Rai Vu
  • 1,595
  • 1
  • 20
  • 30
0

For optimization, I suggest to use @Backs answer with async/await to download the image:

public async Task<Size> GetImageSizeFromUrl(string url)
{
    var imageData = await new System.Net.WebClient().DownloadDataTaskAsync(url);
    var imgStream = new MemoryStream(imageData);
    var img = System.Drawing.Image.FromStream(imgStream);

    return new Size(img.Width, img.Height);
}