Showing posts with label Image. Show all posts
Showing posts with label Image. Show all posts

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);