Wednesday, May 04, 2005

Loading an image residing on a remote web server to a image control.

This has been a question posted to me on an internet site. Initially i thought this had to be simple. But then I realized, in order to do this you need to do a few lines of coding. This was for a Pocket PC application. But anyways coding would not change much for any other platform.

Here it is:

'Give the url as a string
private void LoadImage1(string url, PictureBox pb){
HttpWebRequest wReq = null;
HttpWebResponse wRes = null;

try{
wReq = (HttpWebRequest)WebRequest.Create(url);
wRes = (HttpWebResponse)wReq.GetResponse();
pb.Image = new System.Drawing.Bitmap(wRes.GetResponseStream());
}
catch(Exception ex){
string s = ex.Message;
}
finally {
wRes.Close();
}
}

No comments: