37

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)
Mina Gabriel
  • 23,150
  • 26
  • 96
  • 124

6 Answers6

77

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)
Mina Gabriel
  • 23,150
  • 26
  • 96
  • 124
Varius
  • 3,197
  • 1
  • 19
  • 8
  • If you were worried about the image not being there, would you put the MemoryStream section in a using statement? – Jason Jan 14 '15 at 14:27
  • 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`) – LatentDenis Jul 12 '17 at 20:56
19

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

T_D
  • 3,241
  • 3
  • 17
  • 24
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);
    }
Filix Mogilevsky
  • 727
  • 8
  • 13
  • 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. – Maxim Jan 19 '20 at 09:31
2

iTextSharp is able to accept Uri's:

Image.GetInstance(uri)
VahidN
  • 18,457
  • 8
  • 73
  • 117
1

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()
0
Dim c As New System.Net.WebClient
Dim FileName As String = "c:\StackOverflow.png"
c.DownloadFile(New System.Uri("http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5"), FileName)
Dim img As System.Drawing.Image
img = System.Drawing.Image.FromFile(FileName)
  • Actually, I like his better ^ I was rushing to be first. – blang32 Aug 03 '12 at 19:02
  • 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) – Gian Paolo Jan 19 '20 at 17:28