0

我使用此代码将链接设置为 WPF 图像源:

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

我怎么知道图片链接是否存在?先感谢您!

4

1 回答 1

3
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;
    }
}

如下调用它

bool val = UriExit(bi3.UriSource);
于 2012-06-03T12:59:29.333 回答