2012-03-17 45 views
1

源我想设置图片的来源如下:设置图片来自网络

private void buttonGet_Click(object sender, RoutedEventArgs e) 
    { 
     string website_url =HttpUtility.UrlEncode(textBoxURL.Text); 
     WebClient wc = new WebClient(); 
     wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted); 
     Uri favIconUri = new Uri("http://g.etfv.co/"+ website_url ,UriKind.Absolute); 
     wc.OpenReadAsync(favIconUri, wc); 

    } 
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) 
    { 
     if (e.Error == null && !e.Cancelled) 
     { 
      try 
      { 

       BitmapImage image = new BitmapImage(); 
       image.SetSource(e.Result); 
       image1.Source = image; 
      } 
      catch (Exception ex) 
      { 
       //Exception handle appropriately for your app 
       int i = 0; 
      } 
     } 
     else 
     { 
      //Either cancelled or error handle appropriately for your app 
     } 
    } 
} 

我得到异常: {"The request is not supported. "}符合image.SetSource(e.Result);

文本框网址为“http:// google.com“ 所以形成的网址是:”http://g.etfv.co/http%3a%2f%2fwww.google.com“ 我无法弄清楚一件简单的事情。

我尝试使用简单的URL为“http://img.technospot.net/Windows-Phone-7-Theme-Symbian.jpg”(而不是“http://g.etfv.co/foo-bar “然后它的工作原理,但不是我的方式编码。

任何不正确的?

+0

为什么不只是做新的BitmapImage(favIconUri)? – 2012-03-17 13:54:38

+0

试图..不起作用。 我想展示一些进步等 – 2012-03-17 13:58:27

+1

该图像不显示,也没有在这种情况下抛出一个错误 – 2012-03-17 14:00:52

回答

4

您的问题是返回的图像类型是‘未通过的BitmapSource支持ICO’,只有PNG和JPEG是

其他格式如GIF和ICO只能使用自定义解码器读取。尝试使用不同的服务,以获得图标:

http://www.getfavicon.org/results.php?url=google.com&t=png

会给你一个PNG它的BitmapSource将愉快地加载。

+0

http://www.getfavicon.org/?url=http://www.programmableweb.com/api/ faviconz/favicon.png – 2012-03-18 02:25:51