⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 编程之道VB.NETt程序设计入门-589M.zip
💻 VB
字号:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    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 btnRotateLeft As System.Windows.Forms.Button
    Friend WithEvents btnRotateRight As System.Windows.Forms.Button
    Friend WithEvents btnZoomIn As System.Windows.Forms.Button
    Friend WithEvents btnZoomOut As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents txtX As System.Windows.Forms.TextBox
    Friend WithEvents txtY As System.Windows.Forms.TextBox
    Friend WithEvents txtW As System.Windows.Forms.TextBox
    Friend WithEvents txtH As System.Windows.Forms.TextBox
    Friend WithEvents btnPreview As System.Windows.Forms.Button
    Friend WithEvents btnCrop As System.Windows.Forms.Button
    Friend WithEvents picImage As System.Windows.Forms.PictureBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
        Me.btnRotateLeft = New System.Windows.Forms.Button()
        Me.btnRotateRight = New System.Windows.Forms.Button()
        Me.btnZoomIn = New System.Windows.Forms.Button()
        Me.btnZoomOut = New System.Windows.Forms.Button()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.btnPreview = New System.Windows.Forms.Button()
        Me.txtX = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.txtY = New System.Windows.Forms.TextBox()
        Me.txtW = New System.Windows.Forms.TextBox()
        Me.txtH = New System.Windows.Forms.TextBox()
        Me.btnCrop = New System.Windows.Forms.Button()
        Me.picImage = New System.Windows.Forms.PictureBox()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'btnRotateLeft
        '
        Me.btnRotateLeft.Location = New System.Drawing.Point(56, 16)
        Me.btnRotateLeft.Name = "btnRotateLeft"
        Me.btnRotateLeft.Size = New System.Drawing.Size(80, 24)
        Me.btnRotateLeft.TabIndex = 0
        Me.btnRotateLeft.Text = "向左旋转"
        '
        'btnRotateRight
        '
        Me.btnRotateRight.Location = New System.Drawing.Point(56, 48)
        Me.btnRotateRight.Name = "btnRotateRight"
        Me.btnRotateRight.Size = New System.Drawing.Size(80, 24)
        Me.btnRotateRight.TabIndex = 0
        Me.btnRotateRight.Text = "向右旋转"
        '
        'btnZoomIn
        '
        Me.btnZoomIn.Location = New System.Drawing.Point(56, 80)
        Me.btnZoomIn.Name = "btnZoomIn"
        Me.btnZoomIn.Size = New System.Drawing.Size(80, 24)
        Me.btnZoomIn.TabIndex = 0
        Me.btnZoomIn.Text = "放大"
        '
        'btnZoomOut
        '
        Me.btnZoomOut.Location = New System.Drawing.Point(56, 112)
        Me.btnZoomOut.Name = "btnZoomOut"
        Me.btnZoomOut.Size = New System.Drawing.Size(80, 24)
        Me.btnZoomOut.TabIndex = 0
        Me.btnZoomOut.Text = "缩小"
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnPreview, Me.txtX, Me.Label1, Me.Label2, Me.Label3, Me.Label4, Me.txtY, Me.txtW, Me.txtH, Me.btnCrop})
        Me.GroupBox1.Location = New System.Drawing.Point(208, 8)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(240, 128)
        Me.GroupBox1.TabIndex = 1
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "剪切图像"
        '
        'btnPreview
        '
        Me.btnPreview.Location = New System.Drawing.Point(32, 88)
        Me.btnPreview.Name = "btnPreview"
        Me.btnPreview.Size = New System.Drawing.Size(64, 24)
        Me.btnPreview.TabIndex = 2
        Me.btnPreview.Text = "预览"
        '
        'txtX
        '
        Me.txtX.Location = New System.Drawing.Point(48, 24)
        Me.txtX.Name = "txtX"
        Me.txtX.Size = New System.Drawing.Size(48, 21)
        Me.txtX.TabIndex = 1
        Me.txtX.Text = "0"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(24, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "X"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(136, 24)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(24, 16)
        Me.Label2.TabIndex = 0
        Me.Label2.Text = "Y"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 56)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(32, 16)
        Me.Label3.TabIndex = 0
        Me.Label3.Text = "宽度"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(136, 56)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(32, 16)
        Me.Label4.TabIndex = 0
        Me.Label4.Text = "高度"
        '
        'txtY
        '
        Me.txtY.Location = New System.Drawing.Point(168, 24)
        Me.txtY.Name = "txtY"
        Me.txtY.Size = New System.Drawing.Size(48, 21)
        Me.txtY.TabIndex = 1
        Me.txtY.Text = "0"
        '
        'txtW
        '
        Me.txtW.Location = New System.Drawing.Point(48, 56)
        Me.txtW.Name = "txtW"
        Me.txtW.Size = New System.Drawing.Size(48, 21)
        Me.txtW.TabIndex = 1
        Me.txtW.Text = "200"
        '
        'txtH
        '
        Me.txtH.Location = New System.Drawing.Point(168, 56)
        Me.txtH.Name = "txtH"
        Me.txtH.Size = New System.Drawing.Size(48, 21)
        Me.txtH.TabIndex = 1
        Me.txtH.Text = "200"
        '
        'btnCrop
        '
        Me.btnCrop.Location = New System.Drawing.Point(144, 88)
        Me.btnCrop.Name = "btnCrop"
        Me.btnCrop.Size = New System.Drawing.Size(64, 24)
        Me.btnCrop.TabIndex = 2
        Me.btnCrop.Text = "剪切"
        '
        'picImage
        '
        Me.picImage.Image = CType(resources.GetObject("picImage.Image"), System.Drawing.Bitmap)
        Me.picImage.Location = New System.Drawing.Point(8, 152)
        Me.picImage.Name = "picImage"
        Me.picImage.Size = New System.Drawing.Size(283, 212)
        Me.picImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize
        Me.picImage.TabIndex = 2
        Me.picImage.TabStop = False
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(552, 366)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.picImage, Me.GroupBox1, Me.btnRotateLeft, Me.btnRotateRight, Me.btnZoomIn, Me.btnZoomOut})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnRotateLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRotateLeft.Click
        picImage.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
        picImage.Refresh()
    End Sub

    Private Sub btnRotateRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRotateRight.Click
        picImage.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
        picImage.Refresh()
    End Sub

    Private Sub btnZoomIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZoomIn.Click
        picImage.SizeMode = PictureBoxSizeMode.StretchImage

        picImage.Width = CInt(picImage.Width * 1.25)
        picImage.Height = CInt(picImage.Height * 1.25)
    End Sub

    Private Sub btnZoomOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZoomOut.Click
        picImage.SizeMode = PictureBoxSizeMode.StretchImage

        If picImage.Width > 3 And picImage.Height > 3 Then
            picImage.Width = CInt(picImage.Width * 0.8)
            picImage.Height = CInt(picImage.Height * 0.8)
        End If

    End Sub

    Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
        picImage.Refresh()

        '在剪切区域画一个红色的方框
        Dim recCropBox As New Rectangle(CInt(txtX.Text), _
            CInt(txtY.Text), CInt(txtW.Text), CInt(txtH.Text))
        picImage.CreateGraphics.DrawRectangle(Pens.Red, recCropBox)

    End Sub

    Private Sub btnCrop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrop.Click
        '记录剪切区域
        Dim recCrop As New Rectangle(CInt(txtX.Text), _
            CInt(txtY.Text), CInt(txtW.Text), CInt(txtH.Text))

        '生成一个新的Bitmap,将把剪切后的图像画到这里
        Dim bmpCropped As New Bitmap(CInt(txtW.Text), CInt(txtH.Text))

        '得到一个新的Graphics对象
        Dim grBitmap As Graphics = Graphics.FromImage(bmpCropped)

        '把原始图像在recCrop中的部分画到bmpCropped上
        grBitmap.DrawImage(picImage.Image, 0, 0, recCrop, GraphicsUnit.Pixel)

        '把新的bitmap放到picImage中
        picImage.Image = bmpCropped

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -