I am writing a program to retrieve an image from an existing url. This is my function.
public static Image GetImageFromURL(string url)
{
try
{
WebClient client = new WebClient();
if (url.Contains("url("))
{
url = url.Replace("url(\"", "");
url = url.Replace("\")", "");
}
var tmp = client.DownloadData(url);
return Image.FromStream(new MemoryStream(tmp));
}
catch (Exception e)
{
return null;
}
}
Normally this function works with all images, regardless of file extensions. In most cases, the value of "tmp" variable is at around 100000 or 200000 bytes. However there is a case that the image (after parsed to byte array) contains 635 bytes. This resulted in System.ArgumentException was thrown. The exception message is "Parameter is not valid". It happened when Image.FromStream was called.
Has anyone faced similar problems like this? Can you please advise me what should I do? Thanks.
Anthony