tags:

views:

223

answers:

1

anyone have experience doing this?

+4  A: 

What do you mean with remote? If you mean an image residing on a web server you can do like this:

Dim client As New System.Net.WebClient()
Dim stream As New System.IO.MemoryStream()
Dim data As Byte() = client.DownloadData("http://somewebsite/someimage.jpg")
client.Dispose()
stream.Write(data, 0, data.Length)
pictureBox.Image = Image.FromStream(stream)

Update

Marcs comment about rewinding the stream sparked my curiosity, so I looked into it, and thought I would add it here for completeness.

After writing the data to the stream, the stream's position will be pointing at the end of the stream and before reading from the stream, you would normally need to set the position to the beginning of the stream (stream.Position = 0). As it turns out, Image.FromStream will do this internally, and restore the stream position after loading the image.

Fredrik Mörk
You probably need to rewind the stream between the write and the read - or alternatively, use the MemoryStream ctor that accepts a byte[]
Marc Gravell
cool! what do i have to import to be able to use system.net.webclient?>
I__
marc - can you please elaborate, im a beginner so i dont know what you mean
I__
@Marc: I thought that too, but tested the code before posting and there was no problem with it. Surprised me a bit, honestly.
Fredrik Mörk
fredrik, works beautifully!!!!!!!!
I__
@alex: WebClient is in the System assembly that should be automatically referenced when creating a new project. It is in the System.Net namespace; if you use the code as in my sample no Imports are necessary. If you add Imports System.Net, you can of course leave out the System.Net part when creating the WebClient instance.
Fredrik Mörk
actually i did have to set the position to 0 when i was inserting multiple image in the same stream
I__