# VBForums CodeBank > CodeBank - Visual Basic .NET >  Display image from internet in a Picturebox

## kleinma

VB Code:
'WEB CLIENT IS NEEDED TO DO THE DOWNLOAD
        Dim MyWebClient As New System.Net.WebClient
         'BYTE ARRAY HOLDS THE DATA
        Dim ImageInBytes() As Byte = MyWebClient.DownloadData(TextBox1.Text)
         'CREATE A MEMORY STREAM USING THE BYTES
        Dim ImageStream As New IO.MemoryStream(ImageInBytes)
         'CREATE A BITMAP FROM THE MEMORY STREAM
        PictureBox1.Image = New System.Drawing.Bitmap(ImageStream)
         'AS U SEE, NO FILE NEEDS TO BE WRITTEN TO THE HARD DRIVE, ITS ALL DONE IN MEMORY

Or to merge all that into 1 line of code


VB Code:
PictureBox1.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData(TextBox1.Text)))

Assumes there is a textbox called textbox1 and a picturebox called picturebox1 on a form. Textbox1.text should be the URL of the picture.

This shows how you can grab a picture off the web in .NET 1.1 and display it in a picturebox without actually having to save the image to file first. It uses a memory stream so no data has to be physically written to the drive to then be displayed.

The example uses an image, and a picturebox, but you can see how this could be applied to other file types and used with other classes that can be created via a stream or bytes.

----------


## simoms

thanks, it very useful..

but, do you know how to implement in the smart device?

----------


## kleinma

you mean a device running the compact framework?

----------


## ftr2k6

Kleinma, you are a life saver! Thank you!

----------


## simoms

> you mean a device running the compact framework?


 yes,
I write the same code in the pocketpc, and it show no reference of System.Net.WebClient...that mean i can not use System.Net.WebClient class..

----------


## linz3r

Hi! the code is really useful. However, im having a problem here. In my project, the image from the internet is changed frequently. My project image too, have to be changed. However, whenever the image changes, the url change. Is there a way i can solve this? I tried using substring but to no result. Thanks alot!

----------


## Billcat

the picurebox can show local Gif format image well in VB2005

when i ran this code segment in VB2005, it worked fine if i typed a valid url like "http:// ... /image1.jpg" in textbox,but if i typed a valid url like "http:// ... /image1.gif",a wrong messagebox show:"A generic error occurred in GDI+. ",and the errorcode is -2147467259.

i don't kown how to resolve this problem, please help..

----------


## dynamic_sysop

the WebClient method has always worked well for me over the years on gifs as well as jpgs, but as an alternative if you are having trouble, you could try the HttpWebRequest / HttpWebResponse method.
like this ...

VB Code:
[COLOR=Blue]Dim[/COLOR] req [COLOR=Blue]As[/COLOR] Net.HttpWebRequest = [COLOR=Blue]DirectCast[/COLOR](Net.HttpWebRequest.Create([COLOR=Purple]"http://www.google.co.uk/intl/en_uk/images/logo.gif"[/COLOR]), Net.HttpWebRequest)
        [COLOR=Blue]Dim[/COLOR] res [COLOR=Blue]As[/COLOR] Net.HttpWebResponse = [COLOR=Blue]DirectCast[/COLOR](req.GetResponse, Net.HttpWebResponse)
         [COLOR=Blue]Dim[/COLOR] img [COLOR=Blue]As[/COLOR] Image = Image.FromStream(res.GetResponseStream)
         res.Close()
        '/// StretchImage to make image the same size as picturebox
        '/// AutoSize to make picturebox same size as image
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        PictureBox1.Image = img
that's my personal preffered way of downloading images anyway   :Smilie:

----------


## Billcat

[VB2005] two code segments can display static gif image from internet good, but display animatic gif image form internet ariseing a  mistake

----------


## BrendanDavis

You could also modify this code just a tad to make it a bit more versatile by allowing the option to save the file to disk upon displaying it. I turned it into a function by adjusting a few simple things, like so:


VB Code:
Public Function webDownloadImage(ByVal Url As String, Optional ByVal saveFile As Boolean = False, Optional ByVal location As String = "C:\") As Image
         Dim webClient As New System.Net.WebClient
        Dim bytes() As Byte = webClient.DownloadData(Url)
        Dim stream As New IO.MemoryStream(bytes)
         If saveFile Then My.Computer.FileSystem.WriteAllBytes(location, bytes, False)
         Return New System.Drawing.Bitmap(stream)
     End Function

This way, you can use it as:


VB Code:
PictureBox1.Image = webDownloadImage("http://www.somewebsite.com/image.jpg", True, "C:\temp.jpg")

This will allow you to not only use it as a direct source for an image, but allow you to save the image to your hard drive if you should so choose. Good code, in any case. VERY useful ;D

----------


## egomezpe

(my english is not to good)

hi, finally I found a solution for this .... I don't know if there is a solution anywhere in the forum, but I found that.

I work with Vb .net compact framework, so many properties of .net framework doesn't works

now, this is what I used


VB Code:
Dim req As Net.HttpWebRequest = DirectCast(Net.HttpWebRequest.Create(img_url), Net.HttpWebRequest)
Dim res As Net.HttpWebResponse = DirectCast(req.GetResponse, Net.HttpWebResponse)
Dim img As Image = New System.Drawing.Bitmap(res.GetResponseStream)
res.Close()
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.Image = img

where img_url is the url of the image I want to use, and is a string

I tested it on my PocketPC and it works

thanks

----------


## sapience

> (my english is not to good)
> 
> hi, finally I found a solution for this .... I don't know if there is a solution anywhere in the forum, but I found that.
> 
> I work with Vb .net compact framework, so many properties of .net framework doesn't works
> 
> now, this is what I used
> 
> 
> ...



hi,

im trying to load an image on a mobile device, the above code works fine; however i need to pass in some credentials as the image is located with a protected area of the site and i dont know how to do that.

thanks

----------


## kleinma

> hi,
> 
> im trying to load an image on a mobile device, the above code works fine; however i need to pass in some credentials as the image is located with a protected area of the site and i dont know how to do that.
> 
> thanks


try this:



```
        Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(img_url)
        req.Credentials = New Net.NetworkCredential("usernamehere", "passwordhere")
        Dim res As Net.HttpWebResponse = DirectCast(req.GetResponse, Net.HttpWebResponse)
        Dim img As Image = New System.Drawing.Bitmap(res.GetResponseStream)
        res.Close()
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        PictureBox1.Image = img
```

----------


## CatchItBaby

I'm trying to load an image into a picturebox. The image is on a webpage but I cannot access the direct location of the image. I want to load the website, and have whatever image appears on that website be loaded into a picture box. The image is in .jpg. I cannot simply load the URL the image is at, as the way the webpage is setup I cannot link to the image. The image is generated from a script.

----------


## kleinma

is it a captcha?

----------


## CatchItBaby

no..

It Was Avtar image ie from social netowork site

----------


## sanjal2325

How I can display a picture from a url in excel userform?

Can I use the same code?

I tried but it is giving me some error.

----------


## cheekangteh

> try this:
> 
> 
> 
> ```
>         Dim req As Net.HttpWebRequest = Net.HttpWebRequest.Create(img_url)
>         req.Credentials = New Net.NetworkCredential("usernamehere", "passwordhere")
>         Dim res As Net.HttpWebResponse = DirectCast(req.GetResponse, Net.HttpWebResponse)
>         Dim img As Image = New System.Drawing.Bitmap(res.GetResponseStream)
> ...


hi, i had try this with my own code for my wm6


```
Dim img Image = New.System.Drawing.Bitmap("C:\users\user\Desktop\123.bmp")
PictureBox1.Image = img
```

and i get this error: DirectoryNotFoundExxception was unhandled
could not find a part of the 'C:\users\user\Desktop\123.bmp'.

i double check the picture "123" location, it is correct, but i still get the error code.
i just want to load the picture name "123" to my picturebox in my window mobile 6 professional form.
please help me.

----------


## donPepe

Sorry to bring this post back from the dead but I just wanted to add, 
in some cases you might get a 403 response from the server so you might want to add to the headers of your webclient object the user-agent like this:

objWebClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705 :Wink: ")

That way you save some trouble.

Sorry again for the zombie post.

----------

