Imports System.Drawing.Imaging Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. 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 Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents FileMenu As System.Windows.Forms.MenuItem Friend WithEvents FileOpen As System.Windows.Forms.MenuItem Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents ProcessMenu As System.Windows.Forms.MenuItem Friend WithEvents ProcessEmboss As System.Windows.Forms.MenuItem Friend WithEvents ProcessSharpen As System.Windows.Forms.MenuItem Friend WithEvents ProcessSmooth As System.Windows.Forms.MenuItem Friend WithEvents ViewMenu As System.Windows.Forms.MenuItem Friend WithEvents ViewNormal As System.Windows.Forms.MenuItem Friend WithEvents ViewZoomIn As System.Windows.Forms.MenuItem Friend WithEvents ViewZoomOut As System.Windows.Forms.MenuItem Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents ProcessDiffuse As System.Windows.Forms.MenuItem Friend WithEvents FlipH As System.Windows.Forms.MenuItem Friend WithEvents FlipV As System.Windows.Forms.MenuItem Friend WithEvents RotateLeft As System.Windows.Forms.MenuItem Friend WithEvents RotateRight As System.Windows.Forms.MenuItem Friend WithEvents RotateMenu As System.Windows.Forms.MenuItem 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Private Sub InitializeComponent() Me.ViewNormal = New System.Windows.Forms.MenuItem() Me.RotateLeft = New System.Windows.Forms.MenuItem() Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog() Me.FlipV = New System.Windows.Forms.MenuItem() Me.ProcessMenu = New System.Windows.Forms.MenuItem() Me.ProcessEmboss = New System.Windows.Forms.MenuItem() Me.ProcessSharpen = New System.Windows.Forms.MenuItem() Me.ProcessSmooth = New System.Windows.Forms.MenuItem() Me.ProcessDiffuse = New System.Windows.Forms.MenuItem() Me.MenuItem3 = New System.Windows.Forms.MenuItem() Me.MenuItem5 = New System.Windows.Forms.MenuItem() Me.ViewZoomIn = New System.Windows.Forms.MenuItem() Me.RotateRight = New System.Windows.Forms.MenuItem() Me.ViewMenu = New System.Windows.Forms.MenuItem() Me.ViewZoomOut = New System.Windows.Forms.MenuItem() Me.MenuItem1 = New System.Windows.Forms.MenuItem() Me.MenuItem2 = New System.Windows.Forms.MenuItem() Me.RotateMenu = New System.Windows.Forms.MenuItem() Me.FlipH = New System.Windows.Forms.MenuItem() Me.PictureBox1 = New System.Windows.Forms.PictureBox() Me.FileMenu = New System.Windows.Forms.MenuItem() Me.FileOpen = New System.Windows.Forms.MenuItem() Me.MenuItem4 = New System.Windows.Forms.MenuItem() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.MenuItem6 = New System.Windows.Forms.MenuItem() Me.SuspendLayout() ' 'ViewNormal ' Me.ViewNormal.Index = 0 Me.ViewNormal.Text = "Normal" ' 'RotateLeft ' Me.RotateLeft.Index = 0 Me.RotateLeft.Text = "Rotate Left" ' 'FlipV ' Me.FlipV.Index = 3 Me.FlipV.Text = "Flip Vertical" ' 'ProcessMenu ' Me.ProcessMenu.Index = 1 Me.ProcessMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.ProcessEmboss, Me.ProcessSharpen, Me.ProcessSmooth, Me.ProcessDiffuse, Me.MenuItem3, Me.MenuItem5, Me.MenuItem6}) Me.ProcessMenu.Text = "Process" ' 'ProcessEmboss ' Me.ProcessEmboss.Index = 0 Me.ProcessEmboss.Text = "Emboss" ' 'ProcessSharpen ' Me.ProcessSharpen.Index = 1 Me.ProcessSharpen.Text = "Sharpen" ' 'ProcessSmooth ' Me.ProcessSmooth.Index = 2 Me.ProcessSmooth.Text = "Smooth" ' 'ProcessDiffuse ' Me.ProcessDiffuse.Index = 3 Me.ProcessDiffuse.Text = "Diffuse" ' 'MenuItem3 ' Me.MenuItem3.Index = 4 Me.MenuItem3.Text = "Inverse" ' 'MenuItem5 ' Me.MenuItem5.Index = 5 Me.MenuItem5.Text = "Inverse1" ' 'ViewZoomIn ' Me.ViewZoomIn.Index = 1 Me.ViewZoomIn.Text = "Zoom In" ' 'RotateRight ' Me.RotateRight.Index = 1 Me.RotateRight.Text = "Rotate Right" ' 'ViewMenu ' Me.ViewMenu.Index = 3 Me.ViewMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.ViewNormal, Me.ViewZoomIn, Me.ViewZoomOut}) Me.ViewMenu.Text = "View" ' 'ViewZoomOut ' Me.ViewZoomOut.Index = 2 Me.ViewZoomOut.Text = "Zoom Out" ' 'MenuItem1 ' Me.MenuItem1.Index = 1 Me.MenuItem1.Text = "Save As" ' 'MenuItem2 ' Me.MenuItem2.Index = 3 Me.MenuItem2.Text = "Exit" ' 'RotateMenu ' Me.RotateMenu.Index = 2 Me.RotateMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.RotateLeft, Me.RotateRight, Me.FlipH, Me.FlipV}) Me.RotateMenu.Text = "Rotate" ' 'FlipH ' Me.FlipH.Index = 2 Me.FlipH.Text = "Flip Horizontal" ' 'PictureBox1 ' Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(624, 360) Me.PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False ' 'FileMenu ' Me.FileMenu.Index = 0 Me.FileMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.FileOpen, Me.MenuItem1, Me.MenuItem4, Me.MenuItem2}) Me.FileMenu.Text = "File" ' 'FileOpen ' Me.FileOpen.Index = 0 Me.FileOpen.Text = "Open" ' 'MenuItem4 ' Me.MenuItem4.Index = 2 Me.MenuItem4.Text = "-" ' 'MainMenu1 ' Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.FileMenu, Me.ProcessMenu, Me.RotateMenu, Me.ViewMenu}) ' 'MenuItem6 ' Me.MenuItem6.Index = 6 Me.MenuItem6.Text = "ChangeColor" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.AutoScroll = True Me.ClientSize = New System.Drawing.Size(624, 377) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.PictureBox1}) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "Image Processing" Me.ResumeLayout(False) End Sub #End Region Dim bmap As Bitmap Private Sub FileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileOpen.Click OpenFileDialog1.Filter = "Images|*.BMP;*.GIF;*.TIF;*.JPG" OpenFileDialog1.ShowDialog() If OpenFileDialog1.FileName = "" Then Exit Sub PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName) PictureBox1.Width = PictureBox1.Height * PictureBox1.Image.Width / PictureBox1.Image.Height Me.Text = "(" & PictureBox1.Image.Width.ToString & ", " & PictureBox1.Image.Height.ToString & ")" End Sub Private Sub ProcessEmboss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessEmboss.Click bmap = New Bitmap(PictureBox1.Image) PictureBox1.Image = bmap Dim tempbmp As New Bitmap(PictureBox1.Image) Dim i, j As Integer Dim DispX As Integer = 1, DispY As Integer = 1 Dim red, green, blue As Integer With tempbmp For i = 0 To .Height - 2 For j = 0 To .Width - 2 Dim pixel1, pixel2 As System.Drawing.Color pixel1 = .GetPixel(j, i) pixel2 = .GetPixel(j + DispX, i + DispY) red = Math.Min(Math.Abs(CInt(pixel1.R) - CInt(pixel2.R)) + 128, 255) green = Math.Min(Math.Abs(CInt(pixel1.G) - CInt(pixel2.G)) + 128, 255) blue = Math.Min(Math.Abs(CInt(pixel1.B) - CInt(pixel2.B)) + 128, 255) bmap.SetPixel(j, i, Color.FromArgb(red, green, blue)) Next If i Mod 10 = 0 Then PictureBox1.Invalidate() PictureBox1.Refresh() Me.Text = Int(100 * i / (PictureBox1.Image.Height - 2)).ToString & "%" End If Next End With PictureBox1.Refresh() Me.Text = "Done embossing image" End Sub Private Sub ProcessSharpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessSharpen.Click bmap = New Bitmap(PictureBox1.Image) PictureBox1.Image = bmap Dim tempbmp As New Bitmap(PictureBox1.Image) Dim DX As Integer = 1 Dim DY As Integer = 1 Dim red, green, blue As Integer Dim i, j As Integer With tempbmp For i = DX To .Height - DX - 1 For j = DY To .Width - DY - 1 red = CInt(.GetPixel(j, i).R) + 0.5 * CInt((.GetPixel(j, i).R) - CInt(.GetPixel(j - DX, i - DY).R)) green = CInt(.GetPixel(j, i).G) + 0.7 * CInt((.GetPixel(j, i).G) - CInt(.GetPixel(j - DX, i - DY).G)) blue = CInt(.GetPixel(j, i).B) + 0.5 * CInt((.GetPixel(j, i).B - CInt(.GetPixel(j - DX, i - DY).B))) red = Math.Min(Math.Max(red, 0), 255) green = Math.Min(Math.Max(green, 0), 255) blue = Math.Min(Math.Max(blue, 0), 255) bmap.SetPixel(j, i, Color.FromArgb(red, green, blue)) Next If i Mod 10 = 0 Then PictureBox1.Invalidate() Me.Text = Int(100 * i / (PictureBox1.Image.Height - 2)).ToString & "%" PictureBox1.Refresh() End If Next End With PictureBox1.Refresh() Me.Text = "Done sharpening image" End Sub Private Sub ProcessSmooth_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessSmooth.Click bmap = New Bitmap(PictureBox1.Image) PictureBox1.Image = bmap Dim tempbmp As New Bitmap(PictureBox1.Image) Dim DX As Integer = 1 Dim DY As Integer = 1 Dim red, green, blue As Integer Dim i, j As Integer With tempbmp For i = DX To .Height - DX - 1 For j = DY To .Width - DY - 1 red = CInt((CInt(.GetPixel(j - 1, i - 1).R) + _ CInt(.GetPixel(j - 1, i).R) + _ CInt(.GetPixel(j - 1, i + 1).R) + _ CInt(.GetPixel(j, i - 1).R) + _ CInt(.GetPixel(j, i).R) + _ CInt(.GetPixel(j, i + 1).R) + _ CInt(.GetPixel(j + 1, i - 1).R) + _ CInt(.GetPixel(j + 1, i).R) + _ CInt(.GetPixel(j + 1, i + 1).R)) / 9) green = CInt((CInt(.GetPixel(j - 1, i - 1).G) + _ CInt(.GetPixel(j - 1, i).G) + _ CInt(.GetPixel(j - 1, i + 1).G) + _ CInt(.GetPixel(j, i - 1).G) + _ CInt(.GetPixel(j, i).G) + _ CInt(.GetPixel(j, i + 1).G) + _ CInt(.GetPixel(j + 1, i - 1).G) + _ CInt(.GetPixel(j + 1, i).G) + _ CInt(.GetPixel(j + 1, i + 1).G)) / 9) blue = CInt((CInt(.GetPixel(j - 1, i - 1).B) + _ CInt(.GetPixel(j - 1, i).B) + _ CInt(.GetPixel(j - 1, i + 1).B) + _ CInt(.GetPixel(j, i - 1).B) + _ CInt(.GetPixel(j, i).B) + _ CInt(.GetPixel(j, i + 1).B) + _ CInt(.GetPixel(j + 1, i - 1).B) + _ CInt(.GetPixel(j + 1, i).B) + _ CInt(.GetPixel(j + 1, i + 1).B)) / 9) red = Math.Min(Math.Max(red, 0), 255) green = Math.Min(Math.Max(green, 0), 255) blue = Math.Min(Math.Max(blue, 0), 255) bmap.SetPixel(j, i, Color.FromArgb(red, green, blue)) Next If i Mod 10 = 0 Then PictureBox1.Invalidate() PictureBox1.Refresh() Me.Text = Int(100 * i / (PictureBox1.Image.Height - 2)).ToString & "%" End If Next End With PictureBox1.Refresh() Me.Text = "Done smoothing image" End Sub Private Sub ViewNormal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewNormal.Click PictureBox1.Width = PictureBox1.Image.Width PictureBox1.Height = PictureBox1.Image.Height End Sub Private Sub ViewZoomOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewZoomOut.Click PictureBox1.Width = PictureBox1.Width / 2 PictureBox1.Height = PictureBox1.Height / 2 End Sub Private Sub ViewZoomIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewZoomIn.Click PictureBox1.Width = PictureBox1.Width * 2 PictureBox1.Height = PictureBox1.Height * 2 End Sub Private Sub ProcessDiffuse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessDiffuse.Click bmap = New Bitmap(PictureBox1.Image) PictureBox1.Image = bmap Dim tempbmp As New Bitmap(PictureBox1.Image) Dim i As Integer, j As Integer Dim DX As Integer Dim DY As Integer Dim red As Integer, green As Integer, blue As Integer With tempbmp For i = 3 To .Height - 3 For j = 3 To .Width - 3 DX = Rnd() * 4 - 2 DY = Rnd() * 4 - 2 red = .GetPixel(j + DX, i + DY).R green = .GetPixel(j + DX, i + DY).G blue = .GetPixel(j + DX, i + DY).B bmap.SetPixel(j, i, Color.FromArgb(red, green, blue)) Next Me.Text = Int(100 * i / (.Height - 2)).ToString & "%" If i Mod 10 = 0 Then PictureBox1.Invalidate() PictureBox1.Refresh() Me.Text = Int(100 * i / (PictureBox1.Image.Height - 2)).ToString & "%" End If Next End With PictureBox1.Refresh() Me.Text = "Done diffusing image" End Sub Private Sub RotateLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RotateLeft.Click PictureBox1.Image.RotateFlip(RotateFlipType.Rotate270FlipNone) PictureBox1.Width = PictureBox1.Height * PictureBox1.Image.Width / PictureBox1.Image.Height End Sub Private Sub RotateRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RotateRight.Click PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone) PictureBox1.Width = PictureBox1.Height * PictureBox1.Image.Width / PictureBox1.Image.Height End Sub Private Sub FlipH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlipH.Click PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX) PictureBox1.Refresh() End Sub Private Sub FlipV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FlipV.Click PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipY) PictureBox1.Refresh() End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click Dim R, C As Integer Dim Btmap As New Bitmap(PictureBox1.Image) PictureBox1.Image = Btmap With Btmap For R = 3 To .Width - 3 For C = 0 To .Height - 3 .SetPixel(R, C, RevColor(.GetPixel(R, C))) Next Next Dim d As System.Drawing.Drawing2D.Matrix PictureBox1.Refresh() End With End Sub Function RevColor(ByVal Clr As Color) As Color Return Color.FromArgb(Clr.A, 255 - Clr.R, _ 255 - Clr.G, 255 - Clr.B) End Function Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click Dim Bitmap As Bitmap = New Bitmap(PictureBox1.Image) Dim BitmapData As BitmapData = New BitmapData() Dim Rect As New Rectangle(0, 0, Bitmap.Width, Bitmap.Height) BitmapData = Bitmap.LockBits(Rect, ImageLockMode.WriteOnly, Bitmap.PixelFormat) Dim pixels As IntPtr = BitmapData.Scan0() Dim X, Y, C, O As Integer Dim M As System.Runtime.InteropServices.Marshal Dim W As Integer = Rect.Width Dim Clr As Color For Y = 0 To Rect.Height - 1 For X = 0 To W - 1 O = CInt(Y * W * 4 + X * 4) C = M.ReadInt32(pixels, O) Clr = Color.FromArgb(C) Clr = Color.FromArgb(Clr.A, 255 - Clr.R, 255 - Clr.G, 255 - Clr.B) C = Clr.ToArgb M.WriteInt32(pixels, O, C) Next Next Bitmap.UnlockBits(BitmapData) PictureBox1.Image = Bitmap PictureBox1.Refresh() End Sub Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click Dim image = New Bitmap(PictureBox1.Image) Dim colorMap() As ColorMap = {New ColorMap()} colorMap(0).OldColor = Color.Red colorMap(0).NewColor = Color.Blue Dim imageAttributes As New ImageAttributes() imageAttributes.SetRemapTable(colorMap) Dim W As Integer = PictureBox1.Width Dim H As Integer = PictureBox1.Height Dim G As Graphics G = G.FromImage(image) G.DrawImage(image, New Rectangle(0, 0, W, H), _ 0, 0, W, H, GraphicsUnit.Pixel, imageAttributes) PictureBox1.Image = image PictureBox1.Refresh() End Sub End Class