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

.NET programming topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

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

Post by Neo » Fri Feb 12, 2010 6:11 pm

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
Post Reply

Return to “.NET Programming”