Click here to Skip to main content
15,072,311 members
Articles / Programming Languages / C#
Article
Posted 3 Apr 2008

Tagged as

Stats

132.2K views
28 bookmarked

C# Image Download

Rate me:
Please Sign up or sign in to vote.
1.67/5 (19 votes)
3 Apr 2008CPOL
How to easy download images from the web and save as bitmap supported format.

Introduction

I don't think this small piece of code needs a big introduction. Easy to use for anyone who just started C# programming.

Background

One day I just rattled around and thought to myself it would be nice to have a small easy helper method/class that makes it easy to download images from the web. Here it is. Ready and easy for use.

Using the code

I left out namespace on purpose because I am sure everyone wants to write their own namespace anyway.

Here is the code just to paste into a new class in your IDE:

C#
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);
    }
  }
}

Hope this makes it easier for people.

Points of Interest

Easy and fast use of code?

History

Added .Flush() and .Close().

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ThunderDK
Software Developer
Denmark Denmark
No Biography provided

Comments and Discussions

 
Questioni changed main and includes Pin
michaels24729-Aug-15 22:55
Membermichaels24729-Aug-15 22:55 
Questionimage format? BMP? or JPG? Pin
prashantpatole23-Jun-12 4:09
Memberprashantpatole23-Jun-12 4:09 
AnswerRe: image format? BMP? or JPG? Pin
ThunderDK23-Jun-12 8:46
MemberThunderDK23-Jun-12 8:46 
QuestionCan't Understand Function calling Pin
shahviral22-Feb-12 8:11
professionalshahviral22-Feb-12 8:11 
AnswerRe: Can't Understand Function calling Pin
ThunderDK22-Feb-12 8:41
MemberThunderDK22-Feb-12 8:41 
GeneralMy vote of 4 Pin
mukesh bera25-Jan-12 3:21
Membermukesh bera25-Jan-12 3:21 
GeneralMy vote of 5 Pin
projectzombie11-Dec-11 9:02
Memberprojectzombie11-Dec-11 9:02 
GeneralThanks! Pin
HandyManny27-May-10 11:28
MemberHandyManny27-May-10 11:28 
GeneralMistakes fixed [modified] Pin
ThunderDK5-Apr-08 9:02
MemberThunderDK5-Apr-08 9:02 
GeneralClose the network stream. Pin
Jcmorin3-Apr-08 7:49
MemberJcmorin3-Apr-08 7:49 
GeneralRe: Close the network stream. Pin
zlezj3-Apr-08 9:01
Memberzlezj3-Apr-08 9:01 
GeneralRe: Close the network stream. Pin
Jcmorin3-Apr-08 9:14
MemberJcmorin3-Apr-08 9:14 
GeneralRe: Close the network stream. Pin
Niiiissssshhhhhuuuuu3-Apr-08 11:17
MemberNiiiissssshhhhhuuuuu3-Apr-08 11:17 
GeneralRe: Close the network stream. [modified] Pin
ThunderDK5-Apr-08 9:03
MemberThunderDK5-Apr-08 9:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.