-
Feb 16th, 2006, 04:16 PM
#1
Display image from internet in a Picturebox
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.
Last edited by kleinma; Mar 19th, 2006 at 06:57 PM.
-
Mar 16th, 2006, 04:36 AM
#2
Member
Re: Display image from internet in a Picturebox
thanks, it very useful..
but, do you know how to implement in the smart device?
-
Mar 16th, 2006, 09:20 AM
#3
Re: Display image from internet in a Picturebox
you mean a device running the compact framework?
-
Mar 19th, 2006, 03:58 PM
#4
New Member
Re: Display image from internet in a Picturebox
Kleinma, you are a life saver! Thank you!
-
Mar 20th, 2006, 01:20 AM
#5
Member
Re: Display image from internet in a Picturebox
Originally Posted by kleinma
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..
-
Apr 28th, 2006, 01:16 AM
#6
New Member
Re: Display image from internet in a Picturebox
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!
-
Apr 29th, 2006, 05:39 AM
#7
New Member
Re: Display image from internet in a Picturebox
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..
-
Apr 29th, 2006, 08:56 AM
#8
Re: Display image from internet in a Picturebox
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
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Apr 29th, 2006, 08:16 PM
#9
New Member
Re: Display image from internet in a Picturebox
[VB2005] two code segments can display static gif image from internet good, but display animatic gif image form internet ariseing a mistake
-
Jan 11th, 2007, 09:42 AM
#10
Hyperactive Member
Re: Display image from internet in a Picturebox
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
-
Feb 26th, 2007, 01:51 PM
#11
New Member
Re: Display image from internet in a Picturebox
(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
-
Jun 23rd, 2007, 06:02 AM
#12
New Member
Re: Display image from internet in a Picturebox
Originally Posted by 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
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
-
Jun 23rd, 2007, 12:09 PM
#13
Re: Display image from internet in a Picturebox
Originally Posted by sapience
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:
Code:
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
-
Aug 20th, 2010, 01:58 AM
#14
Hyperactive Member
Re: Display image from internet in a Picturebox
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.
Last edited by CatchItBaby; Aug 20th, 2010 at 02:15 AM.
-
Aug 20th, 2010, 09:52 AM
#15
Re: Display image from internet in a Picturebox
-
Aug 20th, 2010, 02:09 PM
#16
Hyperactive Member
Re: Display image from internet in a Picturebox
no..
It Was Avtar image ie from social netowork site
-
Feb 16th, 2011, 04:24 PM
#17
New Member
Re: Display image from internet in a Picturebox
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.
-
Dec 25th, 2011, 02:24 AM
#18
New Member
Re: Display image from internet in a Picturebox
Originally Posted by kleinma
try this:
Code:
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
hi, i had try this with my own code for my wm6
Code:
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.
-
Jun 11th, 2012, 06:04 PM
#19
New Member
Re: Display image from internet in a Picturebox
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")
That way you save some trouble.
Sorry again for the zombie post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|