I\'m building an application to retrieve an image from internet. Even though it works fine, it is slow (on wrong given URL) when using try-catch statements in the applicatio
Or this source code good image valid optimization:
public static string ValidateImage(string absoluteUrl,string defaultUrl)
{
Uri myUri=null;
if (Uri.TryCreate(absoluteUrl, UriKind.Absolute, out myUri))
{
using (WebClient client = new WebClient())
{
try
{
using (Stream stream = client.OpenRead(myUri))
{
Image image = Image.FromStream(stream);
return (image != null) ? absoluteUrl : defaultUrl;
}
}
catch (ArgumentException)
{
return defaultUrl;
}
catch (WebException)
{
return defaultUrl;
}
}
}
else
{
return defaultUrl;
}
}
Sou and demo asp.net mvc source image created: