Thursday, October 25, 2007

Image Download in .NET CF

Even though it seems more popular to use the WebClient class to download a single image or two, that class is not available on .NET CF. Nonetheless, it's easy enough with WebRequest and WebResponse.

//grab icon
Uri uri = new Uri("valid url to image");
HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
Stream imageStream = httpResponse.GetResponseStream();
Bitmap buddyIcon = new Bitmap(imageStream);
httpResponse.Close();
imageStream.Close();

//save icon as jpeg
string iconFilePath = Path.Combine(FullContactsPath, contact.UserId + ".jpg");
buddyIcon.Save(iconFilePath, ImageFormat.Jpeg);

3 comments:

Anonymous said...

Thanks for this.. it's proved very useful.

Ashu Kumar said...

Great Man.It really works.
a simple and too the point code.
I like it

Pankaj Pareek said...

Hi,Could you tell me how to uplaod the images on website in windows mobile 6.
Regards
john