Click here to Skip to main content
Licence CPOL
First Posted 3 Apr 2008
Views 46,186
Bookmarked 22 times

C# Image Download

By | 3 Apr 2008 | Article
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:

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)

About the Author

ThunderDK

Software Developer

Denmark Denmark

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCan't Understand Function calling Pingroupshahviral8:11 22 Feb '12  
AnswerRe: Can't Understand Function calling PinmemberThunderDK8:41 22 Feb '12  
GeneralMy vote of 4 Pinmembermukesh bera3:21 25 Jan '12  
GeneralMy vote of 5 Pinmemberprojectzombie9:02 11 Dec '11  
GeneralThanks! PinmemberHandyManny11:28 27 May '10  
GeneralMistakes fixed [modified] PinmemberThunderDK9:02 5 Apr '08  
GeneralClose the network stream. PinmemberJcmorin7:49 3 Apr '08  
GeneralRe: Close the network stream. Pinmemberzlezj9:01 3 Apr '08  
GeneralRe: Close the network stream. PinmemberJcmorin9:14 3 Apr '08  
GeneralRe: Close the network stream. PinmemberNiiiissssshhhhhuuuuu11:17 3 Apr '08  
GeneralRe: Close the network stream. [modified] PinmemberThunderDK9:03 5 Apr '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 3 Apr 2008
Article Copyright 2008 by ThunderDK
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid