Page 1 of 1

How to load an image to a PictureBox from web using VB.Net

Posted: Fri Feb 12, 2010 6:11 pm
by Neo
Add the following function and use it as below to load the image from web.

LoadImageFromWeb (PictureBox1, "http://www.yourdomain.com/yourimage.jpg")

Code: Select all

Imports System.IO
Imports System.Net
Imports System.Text

Public Function LoadImageFromWeb(ByVal pb As PictureBox, ByVal ImageURL As String) As Boolean

	Dim objImage As MemoryStream
	Dim objwebClient As WebClient
	Dim sURL As String = Trim(ImageURL)
	Dim bAns As Boolean
	
	Try
		If Not sURL.ToLower().StartsWith("http://") Then sURL = "http://" & sURL
		objwebClient = New WebClient()
		
		objImage = New MemoryStream(objwebClient.DownloadData(sURL))
		pb.Image = Image.FromStream(objImage)
		bAns = True
	Catch ex As Exception
		
		bAns = False
	End Try
	
	Return bAns

End Function