VB.Net Tutorial/2D Graphics/Image Filter

Материал из VB Эксперт
Перейти к: навигация, поиск

Image Filter Effect 1

<source lang="vbnet">"Visual Basic.Net JingCai Programming 100 Examples "Author: Yong Zhang "Publisher: Water Publisher China "ISBN: 750841156 Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ImageFilter1

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
   Public Sub New()
       MyBase.New()
       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
   Private components As System.ruponentModel.IContainer
   Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
   Friend WithEvents Button1 As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.PictureBox1 = New System.Windows.Forms.PictureBox
       Me.Button1 = 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 = Image.FromFile("YourFile.bmp")
       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 = "Effect 1"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(368, 326)
       Me.Controls.Add(Me.Button1)
       Me.Controls.Add(Me.PictureBox1)
       Me.ResumeLayout(False)
   End Sub
   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 = Color.FromArgb(a, rr, gg, bb)
               pic1.SetPixel(j, i, c)
           Next
           PictureBox1.Refresh()
       Next
   End Sub

End Class</source>

Image filter effect 2

<source lang="vbnet">"Visual Basic.Net JingCai Programming 100 Examples "Author: Yong Zhang "Publisher: Water Publisher China "ISBN: 750841156

Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ImageFilter1

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
   Public Sub New()
       MyBase.New()
       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
   Private components As System.ruponentModel.IContainer
   Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
   Friend WithEvents Button1 As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.PictureBox1 = New System.Windows.Forms.PictureBox
       Me.Button1 = 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 = Image.FromFile("YourFile.bmp")
       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 = "Effect 1"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(368, 326)
       Me.Controls.Add(Me.Button1)
       Me.Controls.Add(Me.PictureBox1)
       Me.ResumeLayout(False)
   End Sub
   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
       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 = Color.FromArgb(a, rr, gg, bb)
               pic1.SetPixel(i, j, c)
           Next
           PictureBox1.Refresh()
       Next
   End Sub

End Class</source>