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
2009-06-07 21:25:07
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
2009-06-07 21:28:27
cool! what do i have to import to be able to use system.net.webclient?>
I__
2009-06-07 21:29:50
marc - can you please elaborate, im a beginner so i dont know what you mean
I__
2009-06-07 21:30:20
@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
2009-06-07 21:31:35
fredrik, works beautifully!!!!!!!!
I__
2009-06-07 21:33:18
@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
2009-06-07 21:34:19
actually i did have to set the position to 0 when i was inserting multiple image in the same stream
I__
2009-06-08 19:27:02