SizePic 说明:可以用于WEB网站图片缩略图的批量处理

昨天网友问我怎样获得图片的缩略图,我说简单,但是他说要保持原来的比例,且缩略图的大小是固定的尺寸,我说要思考一会儿。

我数学憋到极点,哎!但是这个我还是算得来

如图

下面是源代码:
'************************************
'* ---------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
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.PictureBox1 = New System.Windows.Forms.PictureBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button2 = New System.Windows.Forms.Button
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.Button3 = New System.Windows.Forms.Button
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.NumericUpDown1 = New System.Windows.Forms.NumericUpDown
        Me.NumericUpDown2 = New System.Windows.Forms.NumericUpDown
        Me.Label3 = New System.Windows.Forms.Label
        Me.Label4 = New System.Windows.Forms.Label
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
        CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.NumericUpDown2, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
        Me.PictureBox1.Location = New System.Drawing.Point(424, 16)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(112, 104)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("Monotype Corsiva", 15.0!, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(320, 112)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(88, 40)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "转换"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(128, 16)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(184, 21)
        Me.TextBox1.TabIndex = 2
        Me.TextBox1.Text = "TextBox1"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(328, 16)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "文件夹"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(128, 56)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(184, 21)
        Me.TextBox2.TabIndex = 4
        Me.TextBox2.Text = "TextBox2"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(328, 56)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(75, 24)
        Me.Button3.TabIndex = 5
        Me.Button3.Text = "保存路径"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(104, 23)
        Me.Label1.TabIndex = 6
        Me.Label1.Text = "图片文件夹路径"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 56)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(80, 23)
        Me.Label2.TabIndex = 7
        Me.Label2.Text = "输出图片路径"
        '
        'NumericUpDown1
        '
        Me.NumericUpDown1.Location = New System.Drawing.Point(64, 32)
        Me.NumericUpDown1.Maximum = New Decimal(New Integer() {22222, 0, 0, 0})
        Me.NumericUpDown1.Name = "NumericUpDown1"
        Me.NumericUpDown1.Size = New System.Drawing.Size(64, 21)
        Me.NumericUpDown1.TabIndex = 8
        Me.NumericUpDown1.Value = New Decimal(New Integer() {70, 0, 0, 0})
        '
        'NumericUpDown2
        '
        Me.NumericUpDown2.Location = New System.Drawing.Point(200, 32)
        Me.NumericUpDown2.Maximum = New Decimal(New Integer() {2222, 0, 0, 0})
        Me.NumericUpDown2.Name = "NumericUpDown2"
        Me.NumericUpDown2.Size = New System.Drawing.Size(64, 21)
        Me.NumericUpDown2.TabIndex = 9
        Me.NumericUpDown2.Value = New Decimal(New Integer() {70, 0, 0, 0})
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(24, 32)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(24, 23)
        Me.Label3.TabIndex = 10
        Me.Label3.Text = "宽"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(152, 32)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(40, 23)
        Me.Label4.TabIndex = 11
        Me.Label4.Text = "高"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.Add(Me.NumericUpDown2)
        Me.GroupBox1.Controls.Add(Me.Label4)
        Me.GroupBox1.Controls.Add(Me.Label3)
        Me.GroupBox1.Controls.Add(Me.NumericUpDown1)
        Me.GroupBox1.Location = New System.Drawing.Point(16, 88)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(296, 64)
        Me.GroupBox1.TabIndex = 12
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "设置需要的尺寸(单位像数)"
        '
        'ProgressBar1
        '
        Me.ProgressBar1.Location = New System.Drawing.Point(416, 128)
        Me.ProgressBar1.Name = "ProgressBar1"
        Me.ProgressBar1.Size = New System.Drawing.Size(120, 16)
        Me.ProgressBar1.TabIndex = 13
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(552, 166)
        Me.Controls.Add(Me.ProgressBar1)
        Me.Controls.Add(Me.GroupBox1)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MinimizeBox = False
        Me.Name = "Form1"
        Me.Text = "SizePic    wgscd @ 2005"
        CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.NumericUpDown2, System.ComponentModel.ISupportInitialize).EndInit()
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim SizeW As Integer = 70
    Dim SizeH As Integer = 70
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SizeW = NumericUpDown1.Value
        SizeH = NumericUpDown2.Value
        doJob()
    End Sub
    Sub doJob()

        Dim dir As New IO.DirectoryInfo(Me.TextBox1.Text)

        Dim img As Bitmap
        Dim f As IO.FileInfo

        Dim i As Integer
        '  Me.ProgressBar1.Maximum = dir.GetFiles.Length

        '  MsgBox("一共有" & dir.GetFiles.Length & "个文件,请稍等处理。。。")

        For Each f In dir.GetFiles


            If f.Extension.ToLower = ".jpg" Or f.Extension.ToLower = ".bmp" Or f.Extension.ToLower = ".png" Or f.Extension.ToLower = ".gif" Then

                img = Image.FromFile(f.FullName)
                Dim w, h As Integer

                w = img.Width
                h = img.Height

                If w > SizeW AndAlso h > SizeH Then


                    If w / h > 1 Then '判断宽和高比例,如果搞高比宽小

                        Dim imgok As New Bitmap(img, (w / h) * SizeW, SizeH) '以高为基本


                        Dim okW, okH, a, g, b As Integer
                        Dim okColor As Color

                        okW = imgok.Width - 1

                        okH = imgok.Height - 1

                        Dim newBitmp As New Bitmap(SizeW, SizeH)
                        Try


                            For iW As Integer = 0 To SizeW - 1
                                For iH As Integer = 0 To SizeH - 1 '以高为基本

                                    'MsgBox(SizeW - 1) '''''''''''
                                    okColor = imgok.GetPixel(iW, iH)


                                    newBitmp.SetPixel(iW, iH, okColor)

                                Next

                            Next

                        Catch ex As Exception

                            MsgBox(ex.ToString)

                        End Try

                        Me.PictureBox1.Image = imgok


                        Me.PictureBox1.Refresh()

                        ' imgok.Save(Me.TextBox2.Text & "/PIC_" & i & ".jpg")
                        Try

                            newBitmp.Save(Me.TextBox2.Text & "/PIC_" & i & ".jpg")

                        Catch ex As Exception

                            MsgBox(ex.ToString)

                        End Try
                    Else

                        Dim imgok As New Bitmap(img, SizeW, (h / w) * SizeW)  '以宽为基本

                        Dim okW, okH, a, g, b As Integer
                        Dim okColor As Color

                        okW = imgok.Width - 1

                        okH = imgok.Height - 1

                        Dim newBitmp As New Bitmap(SizeW, SizeH)
                        Try


                            For iW As Integer = 0 To SizeW - 1
                                For iH As Integer = 0 To SizeH - 1 '以宽为基本


                                    'MsgBox(SizeW - 1) '''''''''''
                                    okColor = imgok.GetPixel(iW, iH)


                                    newBitmp.SetPixel(iW, iH, okColor)

                                Next

                            Next

                        Catch ex As Exception

                            MsgBox(ex.ToString)

                        End Try

                        Me.PictureBox1.Image = imgok

                        Me.PictureBox1.Refresh()

                        ' imgok.Save(Me.TextBox2.Text & "/PIC_" & i & ".jpg")

                        Try

                            newBitmp.Save(Me.TextBox2.Text & "/PIC_" & i & ".jpg")


                        Catch ex As Exception

                            MsgBox(ex.ToString)

                        End Try


                    End If
                End If
            End If

            i += 1
            ProgressBar1.Value += 20
            If ProgressBar1.Value >= 100 Then

                ProgressBar1.Value = 0


            End If


        Next

        MsgBox("完成")
        System.Diagnostics.Process.Start(TextBox2.Text)


        ProgressBar1.Value = 0
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TextBox1.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)

        Me.TextBox2.Text = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim fbd As New FolderBrowserDialog
        If fbd.ShowDialog = DialogResult.OK Then
            Me.TextBox1.Text = fbd.SelectedPath
        End If

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim fbd As New FolderBrowserDialog
        If fbd.ShowDialog = DialogResult.OK Then
            Me.TextBox2.Text = fbd.SelectedPath
        End If
    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click


    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        If e.Button = MouseButtons.Left Then

            MsgBox("wgscd @2005 Copyright")


        End If
    End Sub
End Class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值