如何从URI下载图像并创建位图对象?

14

我正在尝试从一个网站下载图像,并基于该图像创建位图。代码如下:

    public void test()
    {
            PostWebClient client = new PostWebClient(callback);
            cookieContainer = new CookieContainer();
            client.cookies = cookieContainer;
            client.download(new Uri("SITE"));
    }

    public void callback(bool error, string res)
    {
            byte[] byteArray = UnicodeEncoding.UTF8.GetBytes(res);

            MemoryStream stream = new MemoryStream( byteArray );
            var tmp = new BitmapImage();
            tmp.SetSource(stream);
    }

在回调方法的最后一行收到"未指定错误"。有趣的事实是,如果我使用 BitmapImage(new Uri("SITE")),它可以正常工作...(我不能这样做,因为我想从该URL中获取cookies。图像是jpg格式。 PostWebClient类 -> http://paste.org/53413


字节数组的长度是否正确?您能将字节数组的内容转储到文件中并查看图像吗? - flayn
在 Windows Phone .NET 版本中是否有 Image.FromStream 函数? - Pinakin Shah
@Pinakin Shah 不是这样的,这就是为什么我必须创建位图并使用image.source = bitmap。 - Krzysztof Kaczor
@Florian Gerhardt 我会尝试去做这件事情... - Krzysztof Kaczor
明白了!我有类似的解决方案:http://www.nickharris.net/2010/09/asynchronous-image-download-on-windows-phone-7/ - Krzysztof Kaczor
显示剩余2条评论
4个回答

37

这是来自位图类文档的最简代码。

  System.Net.WebRequest request = 
        System.Net.WebRequest.Create(
        "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif");
    System.Net.WebResponse response = request.GetResponse();
    System.IO.Stream responseStream = 
        response.GetResponseStream();
    Bitmap bitmap2 = new Bitmap(responseStream);

Bitmap 的 MSDN 链接


12

最简单的方法是通过WebClient实例打开一个网络流,然后将其传递给Bitmap类的构造函数

using (WebClient wc = new WebClient())
{
    using (Stream s = wc.OpenRead("http://hell.com/leaders/cthulhu.jpg"))
    {
        using (Bitmap bmp = new Bitmap(s))
        {
            bmp.Save("C:\\temp\\octopus.jpg");
        }
    }
}

2
有趣的图片选择。 - S1r-Lanzelot

3
您可以尝试以下代码:
        private Bitmap LoadPicture(string url)
        {
            HttpWebRequest wreq;
            HttpWebResponse wresp;
            Stream mystream;
            Bitmap bmp;

            bmp = null;
            mystream = null;
            wresp = null;
            try
            {
                wreq = (HttpWebRequest)WebRequest.Create(url);
                wreq.AllowWriteStreamBuffering = true;

                wresp = (HttpWebResponse)wreq.GetResponse();

                if ((mystream = wresp.GetResponseStream()) != null)
                    bmp = new Bitmap(mystream);
            }
            finally
            {
                if (mystream != null)
                    mystream.Close();

                if (wresp != null)
                    wresp.Close();
            }
            return (bmp);
        }

0

试试这个:

            string url ="http://www.google.ru/images/srpr/logo11w.png"
            PictureBox picbox = new PictureBox();
            picbox.Load(url);
            Bitmap bitmapRemote = (Bitmap) picbox.Image;

URL - 网络图片,我们创建新的 PictureBox 实例对象,然后调用非异步过程从 URL 加载图像,当图像被检索时,获取位图图像。 此外,您还可以使用线程来处理表单,在其他线程中调用加载并传递委托方法以在完成时检索图像。


3
你能否也加上一个解释? - Robert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接