How can I convert image url to system.drawing.image

60,894

Solution 1

You could use WebClient class to download image and then MemoryStream to read it:

C#

WebClient wc = new WebClient();
byte[] bytes = wc.DownloadData("http://localhost/image.gif");
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);

VB

Dim wc As New WebClient()
Dim bytes As Byte() = wc.DownloadData("http://localhost/image.gif")
Dim ms As New MemoryStream(bytes)
Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(ms)

Solution 2

The other answers are also correct, but it hurts to see the Webclient and MemoryStream not getting disposed, I recommend putting your code in a using.

Example code:

using (var wc = new WebClient())
{
    using (var imgStream = new MemoryStream(wc.DownloadData(imgUrl)))
    {
        using (var objImage = Image.FromStream(imgStream))
        {
            //do stuff with the image
        }
    }
}

The required imports at top of your file are System.IO, System.Net & System.Drawing

In VB.net the syntax was using wc as WebClient = new WebClient() { etc

Solution 3

You can use HttpClient and accomplish this task async with few lines of code.

public async Task<Bitmap> GetImageFromUrl(string url)
    {
        var httpClient = new HttpClient();
        var stream = await httpClient.GetStreamAsync(url);
        return new Bitmap(stream);
    }

Solution 4

iTextSharp is able to accept Uri's:

Image.GetInstance(uri)

Solution 5

You can try this to get the Image

Dim req As System.Net.WebRequest = System.Net.WebRequest.Create("[URL here]")
Dim response As System.Net.WebResponse = req.GetResponse()
Dim stream As Stream = response.GetResponseStream()

Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(stream)
stream.Close()
Share:
60,894
Mina Gabriel
Author by

Mina Gabriel

Programming enthusiast

Updated on January 20, 2020

Comments

  • Mina Gabriel
    Mina Gabriel over 4 years

    I'm using VB.Net I have an url of an image, let's say http://localhost/image.gif

    I need to create a System.Drawing.Image object from that file.

    Notice save this to a file and then open it is not one of my options also i'm using ItextSharp

    here is my code :

    Dim rect As iTextSharp.text.Rectangle
            rect = iTextSharp.text.PageSize.LETTER
            Dim x As PDFDocument = New PDFDocument("chart", rect, 1, 1, 1, 1)
    
            x.UserName = objCurrentUser.FullName
            x.WritePageHeader(1)
            For i = 0 To chartObj.Count - 1
                Dim chartLink as string = "http://localhost/image.gif"
                x.writechart( ** it only accept system.darwing.image ** ) 
    
            Next
    
            x.WritePageFooter()
            x.Finish(False)
    
  • blang32
    blang32 almost 12 years
    Actually, I like his better ^ I was rushing to be first.
  • Mina Gabriel
    Mina Gabriel almost 12 years
    can you tell me how can i do the same think with a page that Response.ContentType = "image/png" i really appreciated
  • Guilherme de Jesus Santos
    Guilherme de Jesus Santos almost 12 years
    I could do it the same way, as a any image, response.GetResponseStream() must work fine.
  • Jason
    Jason over 9 years
    If you were worried about the image not being there, would you put the MemoryStream section in a using statement?
  • LatentDenis
    LatentDenis almost 7 years
    I would upvote this SO HARD if you provided both ASP.Net MVC and ASP.Net Core solution to the C# route - (ASP.Net Core can't use WebClient)
  • Maxim
    Maxim over 4 years
    Please consider adding some explanation or details to your answer. While it might answer the question, just adding a piece of code as an answer, doesn't per say help OP or future community members understand the issue or the proposed solution.
  • Gian Paolo
    Gian Paolo over 4 years
    Save the file is not required (you can work it out using a memory stream) and just add potential problems (say you are within a web application that cannot write in the speficied path)