简体   繁体   中英

No error if WPF Image Source Link doesn't exist

I use this code to set a link as WPF image source:

bi3.BeginInit();
bi3.UriSource = new Uri("[here is a link]" + textBox2.Text + ".png", UriKind.RelativeOrAbsolute);
bi3.CacheOption = BitmapCacheOption.OnLoad;
bi3.EndInit();

How can I know if link to image does exist? Thank you in advance!

private static bool UriExit(Uri uri)
{
    try
    {
        var request = WebRequest.Create(uri) as HttpWebRequest;
        request.Method = "HEAD";
        var response = request.GetResponse() as HttpWebResponse;
        return (response.StatusCode == HttpStatusCode.OK);
    }
    catch
    {
        return false;
    }
}

call it as below

bool val = UriExit(bi3.UriSource);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM