Unable to locate FromStream in Image class

user896692 picture user896692 · Apr 9, 2012 · Viewed 39.1k times · Source

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?

Answer

Rogala picture Rogala · Oct 5, 2013

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

Hopefully this saves you some time, since you can just do a quick copy and paste into your solution.

~Cheers!!