Monday, December 8, 2008

Set image to Picture Box from webpage.

A few months ago I learnt an interesting thing. I have to set an image to a Picture Box from a webpage. For the first time I was a little bit surprised to hear this. But after completing the code, I realized that it was not too hard that I was thinking of.

First declare and initialize an HttpWebRequest object such as:

Dim req As Net.HttpWebRequest = DirectCast(Net.HttpWebRequest.Create(url), Net.HttpWebRequest)

Here url is the respective url of the webpage that contains the image.

Now declare and initialize an HttpWebResponse object such as:

Dim res As Net.HttpWebResponse = DirectCast(req.GetResponse, Net.HttpWebResponse)

Now declare and initialize an Image object such as:

Dim img As Image = New System.Drawing.Bitmap(res.GetResponseStream)
res.Close() 'Closes the response.

Your image is ready. Set the image to the Picture Box:

PictureBox1.Image = img
PictureBox1.Size = PictureBox1.Image.Size

The last line of code changes the size of the Picture Box to the size of the image that is set to the Picture Box.

1 comment:

Movie N Mobile said...

Thanks for visiting my blog.Found your blog very informative.Keep visiting my blog for latest mobile and super hit bollywood movie reviews.