C# Image Download

Posted by Nouman Zakir on Geeks with Blogs See other posts from Geeks with Blogs or by Nouman Zakir
Published on Thu, 11 Mar 2010 20:27:40 GMT Indexed on 2010/03/11 20:44 UTC
Read the original article Hit count: 210

Filed under:
A C# class that makes it easier to download images from the web.  Use the following code in your program to download image files  such as JPG, GIF, PNG, etc from the internet using WebClient class. 


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;

public class DownloadImage {
private string imageUrl;
private Bitmap bitmap;
public DownloadImage(string imageUrl) {
this.imageUrl = imageUrl;
}
public void Download() {
try {
WebClient client = new WebClient();
Stream stream = client.OpenRead(imageUrl);
bitmap = new Bitmap(stream);
stream.Flush();
stream.Close();
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
public Bitmap GetImage() {
return bitmap;
}
public void SaveImage(string filename, ImageFormat format) {
if (bitmap != null) {
bitmap.Save(filename, format);
}
}
}

© Geeks with Blogs or respective owner