2012-06-03 38 views
4

我尝试从Internet链接设置WPF图像源。我怎样才能做到这一点? 我尝试这样做,但不工作:设置来自Hyper Link(来自Internet)的WPF图像源

Image image1 = new Image(); 
BitmapImage bi3 = new BitmapImage(); 
bi3.BeginInit(); 
bi3.UriSource = new Uri("link" + textBox2.Text + ".png", UriKind.Relative); 
bi3.CacheOption = BitmapCacheOption.OnLoad; 
bi3.EndInit(); 

回答

7

预谋"link"到URL肯定是不正确。只要确保你输入图像的完整路径到你的文本框中。

// For example, type the following address into your text box: 
textBox2.Text = "http://www.gravatar.com/avatar/ccac9a107581b343e832a2b040278b98?s=128&d=identicon&r=PG"; 

bi3.UriSource = new Uri(textBox2.Text, UriKind.RelativeOrAbsolute); 
+0

很高兴帮助过!不要忘记用'“http://”'预先添加图片;它有助于.NET知道您的URL的方案。 – Douglas

+0

如何知道链接是否存在?例如,如果我输入“http://ahsdjaksdjahk.png”,它不会给我一个错误。 –

+0

我不确定...一种方法是尝试使用WebClient下载链接并检查响应代码,但我认为有更好的方法。 – Douglas