23

I have the following code:

Image tmpimg = null;
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
return Image.FromStream(stream);

On the last line when I type in Image., FromStream isn't in the list. What can I do?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
user896692
  • 2,351
  • 7
  • 36
  • 57
  • `I´ve got a problem with the found suggestions`. What is that problem? – Oded Apr 09 '12 at 17:38
  • 2
    Read the question. The problem is that FromStream can´t be found – user896692 Apr 09 '12 at 17:41
  • The `FromStream` issue is clear. What was not clear was what deficiencies there were in other answers (or that this was it). – Oded Apr 09 '12 at 17:42
  • When I googled that or read the questions "Download Image from url" always `FromStream` is used. When I try it, it isn´t found – user896692 Apr 09 '12 at 17:43

4 Answers4

28

More detailed out example with using and the namespaces needed.

using System.Net;
using System.IO;
using System.Drawing;

public static Image GetImageFromUrl(string url)
    {
        HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
        
            using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
            {
                using (Stream stream = httpWebReponse.GetResponseStream())
                {
                    return Image.FromStream(stream);
                }
            }
    }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Rogala
  • 2,679
  • 25
  • 27
  • Works like a charm! `Image image1 = GetImageFromUrl("http://upload.wikimedia.org/wikipedia/commons/c/cd/Panda_Cub_from_Wolong,_Sichuan,_China.JPG"); //do something with your image` – Combine May 24 '15 at 08:22
  • Use `var httpWebRequest = HttpWebRequest.CreateHttp(url);` to avoid casting. – m1kael Dec 07 '15 at 23:15
12

try this one:

    using System.Drawing;
    using System.IO;
    using System.Net;

    public static Image GetImageFromUrl(string url)
    {
        using (var webClient = new WebClient())
        {
            return ByteArrayToImage(webClient.DownloadData(url));
        }
    }

    public static Image ByteArrayToImage(byte[] fileBytes)
    {
        using (var stream = new MemoryStream(fileBytes))
        {
            return Image.FromStream(stream);
        }
    }
Sergey Malyutin
  • 1,484
  • 2
  • 20
  • 44
  • 1
    Great point on the byteArray!! I am actually going to need this next week!! Perfect timing. – Rogala Jun 30 '14 at 15:44
8

You probably need using System.Drawing;.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Ok, I had the wrong using. I will accept your answer in 6 minutes. Thanks! – user896692 Apr 09 '12 at 17:45
  • Also you need to close/dispose the http response. Failing to do so leaves the http connection open and trying to make multiple requests to the same server will end up failing. – Will Apr 09 '12 at 18:31
2

btw, you also need to add reference to System.Drawing.dll, only adding using System.Drawing is not enough.

lznt
  • 2,330
  • 2
  • 22
  • 27