[C# .NET] Neopet Image Download Class

Discussion in 'Code Snippets and Tutorials' started by zav75, Aug 10, 2008.

  1. zav75

    zav75 Level I

    Joined:
    Feb 28, 2007
    Messages:
    76
    Likes Received:
    6
    Location:
    Canada, Province of Québec
    This is a class that download an image from neopet, can be useful if you want to understand how it's done.
    I use something similar in my Wrapper to download the haggle image.

    Code (Text):
    1. using System.Net.Sockets;
    2. using System.Drawing;
    3. using System.Drawing.Imaging;
    4. using System.IO;
    5. using System.Net;
    6. using System.ComponentModel;
    7. using System.Data;
    8. using System.Threading;
    9. using System.Collections;
    10.  
    11. namespace AutoBuyer
    12. {
    13.     public class DownloadImage
    14.     {
    15.    
    16.         private Bitmap bitmap;
    17.        
    18.         private string referer_;
    19.         private CookieContainer cookieJar_ = null;
    20.         public DownloadImage(CookieContainer cookieJar, string referer)
    21.         {  
    22.             cookieJar_ = cookieJar;
    23.             referer_ = referer;
    24.         }
    25.         public void Download(string urlImage)
    26.         {
    27.             try
    28.            
    29.             {  
    30.  
    31.                 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlImage);
    32.                 request.CookieContainer = cookieJar_;
    33.                 request.Method = "GET";
    34.                 request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
    35.                 request.Accept = "image/png,image/*;q=0.8,*/*;q=0.5,en;q=0.3";
    36.                 request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
    37.                 request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
    38.                 request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    39.                 request.Headers.Add(HttpRequestHeader.KeepAlive, "300");  
    40.                
    41.                 request.KeepAlive = true;
    42.                 request.Proxy = null;
    43.                 request.Referer = referer_;
    44.                 request.AllowAutoRedirect = false;
    45.  
    46.            
    47.                 Console.WriteLine("download : " +urlImage + " with "+ referer_ + "as referer..");
    48.  
    49.                 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    50.                 System.IO.Stream responseStream =  response.GetResponseStream();
    51.                
    52.                if (response.ContentEncoding.ToLower().Contains("gzip"))
    53.                         responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
    54.                 else if (response.ContentEncoding.ToLower().Contains("deflate"))
    55.                     responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
    56.  
    57.                bitmap = new Bitmap(responseStream);  
    58.                      
    59.                              
    60.             }
    61.             catch (Exception e)
    62.             {
    63.                 Console.WriteLine(e.Message);
    64.  
    65.  
    66.                
    67.  
    68.             }
    69.         }
    70.         public Bitmap GetImage()
    71.         {
    72.             return bitmap;
    73.         }
    74.         public void SaveImage(string filename, ImageFormat format)
    75.         {
    76.             if (bitmap != null)
    77.             {
    78.                 bitmap.Save(filename, format);
    79.             }
    80.         }
    81.     }
    82.  
    83. }
    84.  
    simple and fast.
     
  2. Chris

    Chris Level II

    Joined:
    Aug 16, 2008
    Messages:
    213
    Likes Received:
    2
    Great job on the script. Suprised there are no comments, any reason..? Am I missing something :/
     
  3. zav75

    zav75 Level I

    Joined:
    Feb 28, 2007
    Messages:
    76
    Likes Received:
    6
    Location:
    Canada, Province of Québec

    Hum... I don't know. I guess people don't like C# that much or don't know about it. I find it clearer/easier than VB since it looks like java and C/C++.
    Oh also this isn't a script it a class ^^, scripts are not oriented objet.