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

📄 form1.vb

📁 Source code for VB.NET book - Chapter 4
💻 VB
字号:
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 Button2 As System.Windows.Forms.Button
    <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.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.PictureBox1.Dock = System.Windows.Forms.DockStyle.Top
        Me.PictureBox1.Image = CType(resources.GetObject("PictureBox1.Image"), System.Drawing.Image)
        Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(368, 248)
        Me.PictureBox1.TabIndex = 0
        Me.PictureBox1.TabStop = False
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(72, 280)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(64, 32)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "霓  虹"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(200, 280)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(64, 32)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "浮  雕"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(368, 326)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.PictureBox1)
        Me.Name = "Form1"
        Me.Text = "滤镜"
        Me.ResumeLayout(False)

    End Sub

#End Region
    Dim i, j, rr, gg, bb, r, g, b, a As Integer
    Dim c, cc, cd As System.Drawing.Color
    Dim pic1 As System.Drawing.Bitmap
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        pic1 = PictureBox1.Image
        Dim rr1, gg1, bb1, rr2, gg2, bb2, r1, r2, r3, b1, b2, b3, g1, g2, g3 As Integer
        For j = 0 To PictureBox1.Size.Width - 1
            For i = 0 To PictureBox1.Size.Height - 1
                c = pic1.GetPixel(j, i)
                r1 = c.R
                g1 = c.G
                b1 = c.B
                a = c.A
                cc = pic1.GetPixel(j + 1, i)
                r2 = cc.R
                g2 = cc.G
                b2 = cc.B
                cd = pic1.GetPixel(j, i + 1)
                r3 = cd.R
                g3 = cd.G
                b3 = cd.B
                rr1 = (r1 - r2) ^ 2
                rr2 = (r1 - r3) ^ 2
                rr = 2 * (rr1 + rr2) ^ 0.5
                gg1 = (g1 - g2) ^ 2
                gg2 = (g1 - g3) ^ 2
                gg = 2 * (gg1 + gg2) ^ 0.5
                bb1 = (b1 - b2) ^ 2
                bb2 = (b1 - b3) ^ 2
                bb = 2 * (bb1 + bb2) ^ 0.5
                If rr < 0 Then rr = 0
                If rr > 255 Then rr = 255
                If gg < 0 Then gg = 0
                If gg > 255 Then gg = 255
                If bb < 0 Then bb = 0
                If bb > 255 Then bb = 255
                c = c.FromArgb(a, rr, gg, bb)
                pic1.SetPixel(j, i, c)
            Next
            PictureBox1.Refresh()
        Next
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim rr, gg, bb, i, j, a, r1, r2, b1, b2, g1, g2 As Integer
        pic1 = PictureBox1.Image
        For j = 0 To PictureBox1.Size.Height - 1
            For i = 0 To PictureBox1.Size.Width - 1
                c = pic1.GetPixel(i, j)
                r1 = c.R
                g1 = c.G
                b1 = c.B
                cc = pic1.GetPixel(i + 1, j + 1)
                r2 = cc.R
                g2 = cc.G
                b2 = cc.B
                rr = r2 - r1 + 128
                gg = g2 - g1 + 128
                bb = b2 - b1 + 128
                If rr < 0 Then rr = 0
                If rr > 255 Then rr = 255
                If gg < 0 Then gg = 0
                If gg > 255 Then gg = 255
                If bb < 0 Then bb = 0
                If bb > 255 Then bb = 255
                c = c.FromArgb(a, rr, gg, bb)
                pic1.SetPixel(i, j, c)
            Next
            PictureBox1.Refresh()
        Next
    End Sub
End Class

⌨️ 快捷键说明

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