Image from URL in C#

Here is how you can retrieve a image from a webserver. You only need to know the specific URL, the function does the rest and returns Image object.

// Get image from the given Url
// returns the image or null if not found
public static Image getUrlImage(string psUrl)
{
    WebResponse result = null;
    Image rImage = null;
   
    try
    {
        WebRequest request = WebRequest.Create(psUrl);
        byte[] rBytes;

        // Get the content
        result = request.GetResponse();
        Stream rStream = result.GetResponseStream();

        // Bytes from address
        using( BinaryReader br = new BinaryReader(rStream))
        {
            // Ask for bytes bigger than the actual stream
            rBytes = br.ReadBytes(1000000);
            br.Close();
        }
        // close down the web response object
        result.Close();

        // Bytes into image
        using( MemoryStream imageStream = new MemoryStream(rBytes, 0, rBytes.Length))
        {
            imageStream.Write(rBytes, 0, rBytes.Length);
            rImage = Image.FromStream(imageStream, true);
            imageStream.Close();
        }

    }
    catch (Exception ex)
    {
        Console.WriteLine( “error=” + ex );
    }
    finally
    {
        if (result != null) result.Close();
    }

    return rImage;
}

5 Comments

  1. Tuloko says:

    Hey really usefull function. Thanks

    But now that I’m here, do you know any way to get just the image header and not have to download the whole image?

    I needed to know width and height and downloading an image just for that is a bit wasteful.

    Was trying to find some kind of library that had that functionality but no luck so far.

    Thanks once again 🙂

  2. orn says:

    Something like this, use the sr / streamreader to buffer out as much as you need of the header.

    WebResponse result = null;

    try
    {
    WebRequest req = WebRequest.Create(psUrl);
    req.Method = “GET”;
    req.ContentType = “application/x-www-form-urlencoded”;

    // Has to process the results if the responding service is spitting it out
    result = req.GetResponse();
    Stream ReceiveStream = result.GetResponseStream();
    Encoding encode = System.Text.Encoding.GetEncoding(“utf-8”);
    StreamReader sr = new StreamReader(ReceiveStream, encode);

  3. Prashant says:

    Thanks, Orn Kristjansson,

    Just what we needed was there in your article. It helped us in our work fluently…

  4. Rose says:

    does your code outputs a screen of full length & width or is this a small thumbnail? I am having a problem figuring how to get a thumbnail of a given URL. I am guessing (if this is not it) the idea you showed here is same but since I am new to C# and .NET environment is has been hard for me to figure it out. Any help would be appreciated! Thanks!

  5. orn says:

    The code sample will give you the full Image, if you want to create a thumbnail you could use something like this.

    http://www.csharp-station.com/Articles/Thumbnails.aspx

Leave a Reply