从Web设置图像源

时间:2012-03-17 13:48:31

标签: silverlight windows-phone-7 webclient bitmapimage

我正在尝试按如下方式设置图像来源:

    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. "}行<{1}}

文本框网址为“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”然后它工作但不是我编码的方式。

有什么不对吗?

1 个答案:

答案 0 :(得分:4)

您的问题是返回的图像是“ICO”类型,BitmapSource不支持。仅支持PNG和JPEG。

其他格式(如GIF和ICO)只能使用自定义解码器读取。

尝试使用其他服务来获取favicon:

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

会给你一个PNG,BitmapSource会乐意加载它。