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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12142634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:14:26  来源:igfitidea点击:

How to download an image from an URI and create a bitmap object from it?

c#windows-phone

提问by Krzysztof Kaczor

I'm trying to download image from a website and create bitmap based on that image. It looks like this:

我正在尝试从网站下载图像并基于该图像创建位图。它看起来像这样:

    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);
    }

I receive "Unspecified error" on last line of callback method. Interesting fact is that if I use BitmapImage(new Uri("SITE")) it works well... (I can't do this like that because I want to grab cookies from that URL. The image is an jpg. PostWebClient class -> http://paste.org/53413

我在回调方法的最后一行收到“未指定错误”。有趣的事实是,如果我使用 BitmapImage(new Uri("SITE")) 它运行良好......(我不能这样做,因为我想从那个 URL 获取 cookie。图像是一个 jpg。PostWebClient 类-> http://paste.org/53413

采纳答案by Khurram

This is the simplest code from Bitmap class documentation.

这是位图类文档中最简单的代码。

  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);

MSDN link for Bitmap

位图的 MSDN 链接

回答by Arshad

You can try below code:

你可以试试下面的代码:

        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);
        }

回答by vitalinvent

try this:

尝试这个:

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

url - internet image , we create new instance object PictureBox, then calling NOT ASYNCprocedure to load image from url, when image retrieved get image as bitmap. Also you can use Threadingto work with form, call load in other thread and pass deleate method to retrieve image when complete .

url - 互联网图像,我们创建新的实例对象 PictureBox,然后调用NOT ASYNC过程从 url 加载图像,当检索到图像时将图像作为位图。您也可以使用线程处理表单,在其他线程中调用 load 并在完成时传递 deleate 方法来检索图像。

回答by Jenny O'Reilly

The easiest way is to open a network stream via a WebClientinstance and pass it to the constructorof the Bitmapclass:

最简单的方法是通过打开一个网络流WebClient的实例,并把它传递给构造函数中的位图类:

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");
        }
    }
}