2012-06-03 39 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