Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to locate FromStream in Image class

Tags:

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?

like image 277
user896692 Avatar asked Apr 09 '12 17:04

user896692


2 Answers

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);                 }             }     } 
like image 57
Rogala Avatar answered Oct 25 '22 04:10

Rogala


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);         }     } 
like image 24
Sergey Malyutin Avatar answered Oct 25 '22 03:10

Sergey Malyutin