昨天网友问我怎样获得图片的缩略图,我说简单,但是他说要保持原来的比例,且缩略图的大小是固定的尺寸,我说要思考一会儿。
我数学憋到极点,哎!但是这个我还是算得来
如图
下面是源代码:
'************************************
'* ---------SizePic------------
'说明:可以用于WEB网站图片缩略图的批量处理
'支持jpg,bmp,pnp,gif,tif
' author:wgscd
'* Dte:2005/09/30
'由于用了异常捕捉try,可能效率得到了限制
'部分代码许完善
'QQ:153964481
'*******************************
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents NumericUpDown1 As System.Windows.Forms.NumericUpDown
Friend WithEvents NumericUpDown2 As System.Windows.Forms.NumericUpDown
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
3Me.PictureBox1 = New System.Windows.Forms.PictureBox
4Me.Button1 = New System.Windows.Forms.Button
5Me.TextBox1 = New System.Windows.Forms.TextBox
6Me.Button2 = New System.Windows.Forms.Button
7Me.TextBox2 = New System.Windows.Forms.TextBox
8Me.Button3 = New System.Windows.Forms.Button
9Me.Label1 = New System.Windows.Forms.Label
10Me.Label2 = New System.Windows.Forms.Label
11Me.NumericUpDown1 = New System.Windows.Forms.NumericUpDown
12Me.NumericUpDown2 = New System.Windows.Forms.NumericUpDown
13Me.Label3 = New System.Windows.Forms.Label
14Me.Label4 = New System.Windows.Forms.Label
15Me.GroupBox1 = New System.Windows.Forms.GroupBox
16Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
17CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).BeginInit()
18CType(Me.NumericUpDown2, System.ComponentModel.ISupportInitialize).BeginInit()
19Me.GroupBox1.SuspendLayout()
20Me.SuspendLayout()
21'
22'PictureBox1
23'
24Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
25Me.PictureBox1.Location = New System.Drawing.Point(424, 16)
26Me.PictureBox1.Name = "PictureBox1"
27Me.PictureBox1.Size = New System.Drawing.Size(112, 104)
28Me.PictureBox1.TabIndex = 0
29Me.PictureBox1.TabStop = False
30'
31'Button1
32'
33Me.Button1.Font = New System.Drawing.Font("Monotype Corsiva", 15.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
34Me.Button1.Location = New System.Drawing.Point(320, 112)
35Me.Button1.Name = "Button1"
36Me.Button1.Size = New System.Drawing.Size(88, 40)
37Me.Button1.TabIndex = 1
38Me.Button1.Text = "转换"
39'
40'TextBox1
41'
42Me.TextBox1.Location = New System.Drawing.Point(128, 16)
43Me.TextBox1.Name = "TextBox1"
44Me.TextBox1.Size = New System.Drawing.Size(184, 21)
45Me.TextBox1.TabIndex = 2
46Me.TextBox1.Text = "TextBox1"
47'
48'Button2
49'
50Me.Button2.Location = New System.Drawing.Point(328, 16)
51Me.Button2.Name = "Button2"
52Me.Button2.TabIndex = 3
53Me.Button2.Text = "文件夹"
54'
55'TextBox2
56'
57Me.TextBox2.Location = New System.Drawing.Point(128, 56)
58Me.TextBox2.Name = "TextBox2"
59Me.TextBox2.Size = New System.Drawing.Size(184, 21)
60Me.TextBox2.TabIndex = 4
61Me.TextBox2.Text = "TextBox2"
62'
63'Button3
64'
65Me.Button3.Location = New System.Drawing.Point(328, 56)
66Me.Button3.Name = "Button3"
67Me.Button3.Size = New System.Drawing.Size(75, 24)
68Me.Button3.TabIndex = 5
69Me.Button3.Text = "保存路径"
70'
71'Label1
72'
73Me.Label1.Location = New System.Drawing.Point(16, 16)
74Me.Label1.Name = "Label1"
75Me.Label1.Size = New System.Drawing.Size(104, 23)
76Me.Label1.TabIndex = 6
77Me.Label1.Text = "图片文件夹路径"
78'
79'Label2
80'
81Me.Label2.Location = New System.Drawing.Point(16, 56)
82Me.Label2.Name = "Label2"
83Me.Label2.Size = New System.Drawing.Size(80, 23)
84Me.Label2.TabIndex = 7
85Me.Label2.Text = "输出图片路径"
86'
87'NumericUpDown1
88'
89Me.NumericUpDown1.Location = New System.Drawing.Point(64, 32)
90Me.NumericUpDown1.Maximum = New Decimal(New Integer() {22222, 0, 0, 0})
91Me.NumericUpDown1.Name = "NumericUpDown1"
92Me.NumericUpDown1.Size = New System.Drawing.Size(64, 21)
93Me.NumericUpDown1.TabIndex = 8
94Me.NumericUpDown1.Value = New Decimal(New Integer() {70, 0, 0, 0})
95'
96'NumericUpDown2
97'
98Me.NumericUpDown2.Location = New System.Drawing.Point(200, 32)
99Me.NumericUpDown2.Maximum = New Decimal(New Integer() {2222, 0, 0, 0})
100Me.NumericUpDown2.Name = "NumericUpDown2"
101Me.NumericUpDown2.Size = New System.Drawing.Size(64, 21)
102Me.NumericUpDown2.TabIndex = 9
103Me.NumericUpDown2.Value = New Decimal(New Integer() {70, 0, 0, 0})
104'
105'Label3
106'
107Me.Label3.Location = New System.Drawing.Point(24, 32)
108Me.Label3.Name = "Label3"
109Me.Label3.Size = New System.Drawing.Size(24, 23)
110Me.Label3.TabIndex = 10
111Me.Label3.Text = "宽"
112'
113'Label4
114'
115Me.Label4.Location = New System.Drawing.Point(152, 32)
116Me.Label4.Name = "Label4"
117Me.Label4.Size = New System.Drawing.Size(40, 23)
118Me.Label4.TabIndex = 11
119Me.Label4.Text = "高"
120'
121'GroupBox1
122'
123Me.GroupBox1.Controls.Add(Me.NumericUpDown2)
124Me.GroupBox1.Controls.Add(Me.Label4)
125Me.GroupBox1.Controls.Add(Me.Label3)
126Me.GroupBox1.Controls.Add(Me.NumericUpDown1)
127Me.GroupBox1.Location = New System.Drawing.Point(16, 88)
128Me.GroupBox1.Name = "GroupBox1"
129Me.GroupBox1.Size = New System.Drawing.Size(296, 64)
130Me.GroupBox1.TabIndex = 12
131Me.GroupBox1.TabStop = False
132Me.GroupBox1.Text = "设置需要的尺寸(单位像数)"
133'
134'ProgressBar1
135'
136Me.ProgressBar1.Location = New System.Drawing.Point(416, 128)
137Me.ProgressBar1.Name = "ProgressBar1"
138Me.ProgressBar1.Size = New System.Drawing.Size(120, 16)
139Me.ProgressBar1.TabIndex = 13
140'
141'Form1
142'
143Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
144Me.ClientSize = New System.Drawing.Size(552, 166)
145Me.Controls.Add(Me.ProgressBar1)
146Me.Controls.Add(Me.GroupBox1)
147Me.Controls.Add(Me.Label2)
148Me.Controls.Add(Me.Label1)
149Me.Controls.Add(Me.Button3)
150Me.Controls.Add(Me.TextBox2)
151Me.Controls.Add(Me.Button2)
152Me.Controls.Add(Me.TextBox1)
153Me.Controls.Add(Me.Button1)
154Me.Controls.Add(Me.PictureBox1)
155Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
156Me.MinimizeBox = False
157Me.Name = "Form1"
158Me.Text = "SizePic wgscd @ 2005"
159CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).EndInit()
160CType(Me.NumericUpDown2, System.ComponentModel.ISupportInitialize).EndInit()
161Me.GroupBox1.ResumeLayout(False)
162Me.ResumeLayout(False)
163
164End Sub
165
166#End Region
167Dim SizeW As Integer = 70
168Dim SizeH As Integer = 70
169Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
170SizeW = NumericUpDown1.Value
171SizeH = NumericUpDown2.Value
172doJob()
173End Sub
174Sub doJob()
175
176Dim dir As New IO.DirectoryInfo(Me.TextBox1.Text)
177
178Dim img As Bitmap
179Dim f As IO.FileInfo
180
181Dim i As Integer
182' Me.ProgressBar1.Maximum = dir.GetFiles.Length
183
184' MsgBox("一共有" & dir.GetFiles.Length & "个文件,请稍等处理。。。")
185
186For Each f In dir.GetFiles
187
188
189If f.Extension.ToLower = ".jpg" Or f.Extension.ToLower = ".bmp" Or f.Extension.ToLower = ".png" Or f.Extension.ToLower = ".gif" Then
190
191img = Image.FromFile(f.FullName)
192Dim w, h As Integer
193
194w = img.Width
195h = img.Height
196
197If w > SizeW AndAlso h > SizeH Then
198
199
200If w / h > 1 Then '判断宽和高比例,如果搞高比宽小
201
202Dim imgok As New Bitmap(img, (w / h) * SizeW, SizeH) '以高为基本
203
204
205Dim okW, okH, a, g, b As Integer
206Dim okColor As Color
207
208okW = imgok.Width - 1
209
210okH = imgok.Height - 1
211
212Dim newBitmp As New Bitmap(SizeW, SizeH)
213Try
214
215
216For iW As Integer = 0 To SizeW - 1
217For iH As Integer = 0 To SizeH - 1 '以高为基本
218
219'MsgBox(SizeW - 1) '''''''''''
220okColor = imgok.GetPixel(iW, iH)
221
222
223newBitmp.SetPixel(iW, iH, okColor)
224
225Next
226
227Next
228
229Catch ex As Exception
230
231MsgBox(ex.ToString)
232
233End Try
234
235Me.PictureBox1.Image = imgok
236
237
238Me.PictureBox1.Refresh()
239
240' imgok.Save(Me.TextBox2.Text & "\PIC_" & i & ".jpg")
241Try
242
243newBitmp.Save(Me.TextBox2.Text & "\PIC_" & i & ".jpg")
244
245Catch ex As Exception
246
247MsgBox(ex.ToString)
248
249End Try
250Else
251
252Dim imgok As New Bitmap(img, SizeW, (h / w) * SizeW) '以宽为基本
253
254Dim okW, okH, a, g, b As Integer
255Dim okColor As Color
256
257okW = imgok.Width - 1
258
259okH = imgok.Height - 1
260
261Dim newBitmp As New Bitmap(SizeW, SizeH)
262Try
263
264
265For iW As Integer = 0 To SizeW - 1
266For iH As Integer = 0 To SizeH - 1 '以宽为基本
267
268
269'MsgBox(SizeW - 1) '''''''''''
270okColor = imgok.GetPixel(iW, iH)
271
272
273newBitmp.SetPixel(iW, iH, okColor)
274
275Next
276
277Next
278
279Catch ex As Exception
280
281MsgBox(ex.ToString)
282
283End Try
284
285Me.PictureBox1.Image = imgok
286
287Me.PictureBox1.Refresh()
288
289' imgok.Save(Me.TextBox2.Text & "\PIC_" & i & ".jpg")
290
291Try
292
293newBitmp.Save(Me.TextBox2.Text & "\PIC_" & i & ".jpg")
294
295
296Catch ex As Exception
297
298MsgBox(ex.ToString)
299
300End Try
301
302
303End If
304End If
305End If
306
307i += 1
308ProgressBar1.Value += 20
309If ProgressBar1.Value >= 100 Then
310
311ProgressBar1.Value = 0
312
313
314End If
315
316
317Next
318
319MsgBox("完成")
320System.Diagnostics.Process.Start(TextBox2.Text)
321
322
323ProgressBar1.Value = 0
324End Sub
325Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
326Me.TextBox1.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
327
328Me.TextBox2.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
329
330
331End Sub
332
333Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
334Dim fbd As New FolderBrowserDialog
335If fbd.ShowDialog = DialogResult.OK Then
336Me.TextBox1.Text = fbd.SelectedPath
337End If
338
339End Sub
340
341Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
342Dim fbd As New FolderBrowserDialog
343If fbd.ShowDialog = DialogResult.OK Then
344Me.TextBox2.Text = fbd.SelectedPath
345End If
346End Sub
347
348Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
349
350End Sub
351
352Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
353
354
355End Sub
356
357Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
358
359If e.Button = MouseButtons.Left Then
360
361MsgBox("wgscd @2005 Copyright")
362
363
364End If
365End Sub
366End Class</system.diagnostics.debuggerstepthrough()>