简体   繁体   English

如果WPF图像源链接不存在,则没有错误

[英]No error if WPF Image Source Link doesn't exist

I use this code to set a link as WPF image source: 我使用以下代码将链接设置为WPF图像源:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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