1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.IO;
10 using System.Threading;
11
12 namespace WindowsFormsApplication1
13 {
14 public partial class Form1 : Form
15 {
16
17 public delegate void MyInvoke(object str);
18
19 public Form1()
20 {
21 InitializeComponent();
22 }
23
24 public void setProgress(object iValue )
25 {
26 if( progressBar1.InvokeRequired )
27 {
28 MyInvoke inv = new MyInvoke(setProgress);
29 this.Invoke(inv, new Object[] { iValue });
30 }
31 else{
32 progressBar1.Value = (int)iValue;
33 }
34 }
35
36 public void setMaximum(object iValue)
37 {
38 if (progressBar1.InvokeRequired)
39 {
40 MyInvoke inv = new MyInvoke(setMaximum);
41 this.Invoke(inv, new Object[] { iValue });
42 }
43 else
44 {
45 progressBar1.Minimum = 0;
46 progressBar1.Maximum = (int)iValue;
47 progressBar1.Value = 0;
48 }
49 }
50
51 public void EnableButton(object iValue)
52 {
53 if (button1.InvokeRequired)
54 {
55 MyInvoke inv = new MyInvoke(EnableButton);
56 this.Invoke(inv, new Object[] { iValue });
57 }
58 else
59 {
60 button1.Enabled = (bool)iValue;
61 }
62 }
63
64
65 public void DoWork(object thObj)
66 {
67 ResizeMethod((string)thObj);
68 EnableButton(true);
69
70 string str = string.Format("共转换完成图片{0}个!", progressBar1.Maximum);
71
72 MessageBox.Show(str);
73 }
74
75
76
77
78 private void button1_Click(object sender, EventArgs e)
79 {
80 FolderBrowserDialog dlg = new FolderBrowserDialog();
81 if( dlg.ShowDialog() == DialogResult.OK )
82 {
83 string strPath = dlg.SelectedPath;
84 Thread thread = new Thread(new ParameterizedThreadStart(DoWork));
85 thread.Start(strPath);
86 }
87
88 }
89
90 private void ResizeMethod(string strPath)
91 {
92 string[] strFiles = Directory.GetFiles(strPath, "*.jpg", SearchOption.TopDirectoryOnly);
93
94
95
96 setMaximum( strFiles.Length );
97 EnableButton(false);
98 int iCount = 0;
99
100
101 foreach (string ss in strFiles)
102 {
103 try
104 {
105 var img = Image.FromFile(ss);
106
107
108 double blk = (img.Width + 0.0) / 1024.0;
109
110 int newHeight = (int)(img.Height / blk);
111
112 Bitmap tempBitmap = new Bitmap(1024, newHeight);
113
114 Graphics gTempBitmap = System.Drawing.Graphics.FromImage(tempBitmap);
115
116 gTempBitmap.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
117 gTempBitmap.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
118 gTempBitmap.Clear(Color.White);
119
120
121 gTempBitmap.DrawImage(img, new Rectangle(0, 0, 1024, newHeight), new Rectangle(0, 0, img.Width, img.Height),
122 GraphicsUnit.Pixel);
123
124
125 tempBitmap.Save(@"d:\img\" + Path.GetFileName(ss));
126
127 gTempBitmap.Dispose();
128 img.Dispose();
129 tempBitmap.Dispose();
130
131
132 if( iCount % 10 == 0 )
133 {
134 GC.Collect();
135 }
136
137
138 }
139 catch (System.Exception e1)
140 {
141 MessageBox.Show(e1.Message);
142 }
143 finally
144 {
145 iCount++;
146 setProgress(iCount);
147 }
148
149 }
150 }
151 }
152 }