No se puede localizar FromStream en la clase de imagen


Tengo el siguiente código:

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

En la última línea cuando escribo Image., FromStream no está en la lista. ¿Qué puedo hacer?

Author: gunr2171, 2012-04-09

4 answers

Probablemente necesites using System.Drawing;.

 8
Author: SLaks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-09 17:39:37

Ejemplo más detallado con el uso y los espacios de nombres necesarios.

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

Esperemos que esto le ahorre algo de tiempo, ya que solo puede hacer una copia rápida y pegar en su solución.

~ Salud!!

 28
Author: Rogala,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-06 14:14:31

Prueba este:

    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);
        }
    }
 11
Author: Sergey Malyutin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-05 10:31:48

Por cierto, también necesita agregar referencia al sistema.Dibujo.dll, solo agregando usando el sistema.Dibujar no es suficiente.

 2
Author: lznt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-01 23:24:25