我使用此代码将链接设置为 WPF 图像源:
bi3.BeginInit();
bi3.UriSource = new Uri("[here is a link]" + textBox2.Text + ".png", UriKind.RelativeOrAbsolute);
bi3.CacheOption = BitmapCacheOption.OnLoad;
bi3.EndInit();
我怎么知道图片链接是否存在?先感谢您!
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);