Unable to locate FromStream in Image class
Asked Answered
T

4

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?

Tameshatamez answered 9/4, 2012 at 17:37 Comment(4)
I´ve got a problem with the found suggestions. What is that problem?Sutherland
Read the question. The problem is that FromStream can´t be foundTameshatamez
The FromStream issue is clear. What was not clear was what deficiencies there were in other answers (or that this was it).Sutherland
When I googled that or read the questions "Download Image from url" always FromStream is used. When I try it, it isn´t foundTameshatamez
W
8

You probably need using System.Drawing;.

Wotan answered 9/4, 2012 at 17:39 Comment(2)
Ok, I had the wrong using. I will accept your answer in 6 minutes. Thanks!Tameshatamez
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.Zoller
T
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);
                }
            }
    }
Thomasinethomason answered 5/10, 2013 at 13:29 Comment(2)
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 imageVizcacha
Use var httpWebRequest = HttpWebRequest.CreateHttp(url); to avoid casting.Parliamentarianism
O
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);
        }
    }
Order answered 5/4, 2014 at 10:26 Comment(1)
Great point on the byteArray!! I am actually going to need this next week!! Perfect timing.Thomasinethomason
W
8

You probably need using System.Drawing;.

Wotan answered 9/4, 2012 at 17:39 Comment(2)
Ok, I had the wrong using. I will accept your answer in 6 minutes. Thanks!Tameshatamez
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.Zoller
P
3

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

Pharyngology answered 1/8, 2014 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.