C# Image Download

孟文栋
2023-12-01
 
  
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Drawing;
5 using System.Drawing.Imaging;
6 using System.IO;
7 using System.Net;
8
9
10 namespace COMP
11 {
12 public class DownloadImage
13 {
14 private string imageUrl;
15 private Bitmap bitmap;
16 /// <summary>
17 /// 涂聚文 2011-05-05
18 ///
19 /// </summary>
20 /// <param name="imageUrl"></param>
21 public DownloadImage( string imageUrl)
22 {
23 this .imageUrl = imageUrl;
24 }
25 /// <summary>
26 ///
27 /// </summary>
28 public void Download()
29 {
30 try
31 {
32 WebClient client = new WebClient();
33 Stream stream = client.OpenRead(imageUrl);
34 bitmap = new Bitmap(stream);
35 stream.Flush();
36 stream.Close();
37 }
38 catch (Exception e)
39 {
40 Console.WriteLine(e.Message);
41 }
42 }
43 /// <summary>
44 ///
45 /// </summary>
46 /// <returns></returns>
47 public Bitmap GetImage()
48 {
49 return bitmap;
50 }
51 /// <summary>
52 ///
53 /// </summary>
54 /// <param name="filename"></param>
55 /// <param name="format"></param>
56 public void SaveImage( string filename, ImageFormat format)
57 {
58 if (bitmap != null )
59 {
60 bitmap.Save(filename, format);
61 }
62 }
63
64 }
65 }
 类似资料: