I want to check size image from url in C#.
Ex: Url: http://img.khoahoc.tv/photos/image/2015/05/14/hang_13.jpg
I want to check size image from url in C#.
Ex: Url: http://img.khoahoc.tv/photos/image/2015/05/14/hang_13.jpg
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;
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);
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);
}