menu

Showing posts with label Visual Basic. Show all posts
Showing posts with label Visual Basic. Show all posts

Visual Basic - Image Processing 6 (convert)


Hello everyone, akhirnya sampai juga kita pada tutorial terakhir dari bab image processing ini, selanjutnya akan dijelaskan lebih lanjut tentang Jaringan Saraf Tiruan.

Pada tutorial kali ini, kita akan membuat sebuah engine yang mampu mengconvert format suatu image menjadi format yang lain,

Tutorial kali ini merupakan lanjutan tutorial sebelumnya tentang Edge Detection, jika belum paham tentang Visual Basic baca disini.

Oke langsung saja kita mulai tutorialnya,

Pertama, buat sebuah User Interface menggunakan tools PictureBox, ComboBox, Button, OpenFileDialog, dan SaveFileDialog sehingga hasilnya akan tampak seperti ini,


Untuk membuat ComboBox, tambahkan itemsnya pada items (collection) dan tampilannya akan tampak seperti ini,


Selanjutnya klik 2x Button Select dan ketikkan code berikut,
OpenFileDialog1.Title = "Masukkan Gambar"
OpenFileDialog1.Filter = "image file (*.jpg) |*.jpg| image file (*.psd) |*.psd| image file (*.png) |*.png| image file (*.gif) |*.gif| image file (*.bmp) |*.bmp| all files (*.*) | *.*"
OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
    PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If

Kemudian klik 2x Button Convert dan ketikkan code berikut,
        If ComboBox1.SelectedItem = "JPEG" Then
            Try
                SaveFileDialog1.Filter = "JPEG |*.jpeg"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        If ComboBox1.SelectedItem = "PSD" Then
            Try
                SaveFileDialog1.Filter = "PSD |*.psd"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        If ComboBox1.SelectedItem = "PNG" Then
            Try
                SaveFileDialog1.Filter = "PNG |*.png"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        If ComboBox1.SelectedItem = "GIF" Then
            Try
                SaveFileDialog1.Filter = "GIF |*.gif"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        If ComboBox1.SelectedItem = "BMP" Then
            Try
                SaveFileDialog1.Filter = "BMP |*.bmp"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        MsgBox("Success")

Sehingga fullcodenya akan tampak seperti ini,
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Title = "Masukkan Gambar"
        OpenFileDialog1.Filter = "image file (*.jpg) |*.jpg| image file (*.psd) |*.psd| image file (*.png) |*.png| image file (*.gif) |*.gif| image file (*.bmp) |*.bmp| all files (*.*) | *.*"
        OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If ComboBox1.SelectedItem = "JPEG" Then
            Try
                SaveFileDialog1.Filter = "JPEG |*.jpeg"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        If ComboBox1.SelectedItem = "PSD" Then
            Try
                SaveFileDialog1.Filter = "PSD |*.psd"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        If ComboBox1.SelectedItem = "PNG" Then
            Try
                SaveFileDialog1.Filter = "PNG |*.png"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        If ComboBox1.SelectedItem = "GIF" Then
            Try
                SaveFileDialog1.Filter = "GIF |*.gif"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        If ComboBox1.SelectedItem = "BMP" Then
            Try
                SaveFileDialog1.Filter = "BMP |*.bmp"
                If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                End If
            Catch ex As Exception
            End Try
        End If
        MsgBox("Success")
    End Sub

    Private Sub Form1_load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.SelectedItem = "JPEG"
    End Sub
End Class

Dan tampilan akhirnya akan tampak seperti ini,



===DONE!===

Visual Basic - Image Processing 5 (edge detection)


Lanjut lagi image processing,,

Pada tutorial sebelumnya kita sudah sampai pada membuat sebuah engine yang mampu meng-greyscale image, tutorial kali ini kita akan membuat sebuah engine kembali yang mampu mendeteksi garis tepi suatu citra atau bisa disebut sebagai Edge Detection,

Belum begitu mengerti tentang Visual Studion? bisa lihat disini,
Oke langsung saja kita mulai prakteknya,,

Pertama buat dulu User Interfacenya menggunakan, GroupBox, PictureBox, OpenFileDialog, SaveFileDialog, dan Button, sehingga hasilnya akan tampak seperti ini,


Selanjutnya klik 2x Button Open dan ketikkan code berikut,
OpenFileDialog1.Filter = "Images|*.GIF; *.TIF;*.JPG;*BMP"
OpenFileDialog1.ShowDialog()
If OpenFileDialog1.FileName = "" Then Exit Sub
PicAda = True
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
PictureBox2.Image = Image.FromFile(OpenFileDialog1.FileName)

Kemudian klik 2x Button Process dan ketikkan code berikut,
If PicAda = False Then
     MsgBox("Pilih dulu gambar yang akan diproses", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Proses gagal")
     Exit Sub
End If
Dim source As New Bitmap(PictureBox1.Image)
Dim sobelResult As New Bitmap(PictureBox1.Image)

Dim sobelX, sobelY, magnitude As Integer

Dim neighbourList As ArrayList = New ArrayList
Call Greyscale()
For y As Integer = 0 To source.Height - 1
     For x As Integer = 0 To source.Width - 1
         neighbourList.Clear()

         neighbourList = getThreeNeighbourList(x, y, source)

         sobelX = getSobelValue(neighbourList, "X")
         sobelY = getSobelValue(neighbourList, "Y")

         magnitude = Math.Sqrt(Math.Pow(sobelX, 2) + Math.Pow(sobelY, 2))

         If magnitude > 150 Then
             magnitude = 255
         Else
             magnitude = 0
         End If

         sobelResult.SetPixel(x, y, Color.FromArgb(magnitude, magnitude, magnitude))
     Next x
Next y

PictureBox2.Image = sobelResult
MessageBox.Show("Proses Sobel Telah Selesai")

Setelah itu klik 2x Button Save dan ketikkan code berikut,
Try
    SaveFileDialog1.Filter = "JPEG |*.jpg"
    If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        PictureBox2.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
    End If
Catch ex As Exception
End Try

Terakhir ketikkan code edge detection berikut,
     Public Function getSobelValue(ByVal neighbourList As ArrayList, ByVal maskType As String) As Integer
        Dim result As Integer = 0

        Dim sobelX As Integer(,) = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}
        Dim sobelY As Integer(,) = {{1, 2, 1}, {0, 0, 0}, {-1, -2, -1}}

        Dim count As Integer = 0

        If (maskType.Equals("X")) Then
            For y As Integer = 0 To 2
                For x As Integer = 0 To 2
                    result = result + (sobelX(x, y) * Convert.ToInt16(neighbourList(count)))

                    count = count + 1
                Next x
            Next y
        ElseIf (maskType.Equals("Y")) Then
            For y As Integer = 0 To 2
                For x As Integer = 0 To 2
                    result = result + (sobelY(x, y) * Convert.ToInt16(neighbourList(count)))

                    count = count + 1
                Next x
            Next y
        End If

        Return result
    End Function

    Public Function getThreeNeighbourList(ByVal xPos As Integer, ByVal yPos As Integer, ByVal source As Bitmap) As ArrayList
        Dim neighbourList As ArrayList = New ArrayList

        Dim xStart, xFinish, yStart, yFinish As Integer

        Dim pixel As Integer

        xStart = xPos - 1
        xFinish = xPos + 1

        yStart = yPos - 1
        yFinish = yPos + 1

        For y As Integer = yStart To yFinish
            For x As Integer = xStart To xFinish
                If (x < 0) Or (y < 0) Or (x > source.Width - 1) Or (y > source.Height - 1) Then
                    neighbourList.Add(0)
                Else
                    pixel = source.GetPixel(x, y).R

                    neighbourList.Add(pixel)
                End If
            Next x
        Next y

        Return neighbourList
    End Function
    Private Sub Greyscale()
        Dim source As New Bitmap(PictureBox1.Image)

        Dim red, green, blue, grayscale As Integer

        For y As Integer = 0 To source.Height - 1
            For x As Integer = 0 To source.Width - 1
                red = source.GetPixel(x, y).R
                green = source.GetPixel(x, y).G
                blue = source.GetPixel(x, y).B

                grayscale = red * 0.299 + green * 0.587 + blue * 0.114

                source.SetPixel(x, y, Color.FromArgb(grayscale, grayscale, grayscale))
            Next x
        Next y

        PictureBox1.Image = source
    End Sub

Sehingga full codenya akan tampak seperti ini,
Public Class Form1
    Dim PicAda As Boolean
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "Images|*.GIF; *.TIF;*.JPG;*BMP"
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName = "" Then Exit Sub
        PicAda = True
        PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        PictureBox2.Image = Image.FromFile(OpenFileDialog1.FileName)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If PicAda = False Then
            MsgBox("Pilih dulu gambar yang akan diproses", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Proses gagal")
            Exit Sub
        End If
        Dim source As New Bitmap(PictureBox1.Image)
        Dim sobelResult As New Bitmap(PictureBox1.Image)

        Dim sobelX, sobelY, magnitude As Integer

        Dim neighbourList As ArrayList = New ArrayList
        Call Greyscale()
        For y As Integer = 0 To source.Height - 1
            For x As Integer = 0 To source.Width - 1
                neighbourList.Clear()

                neighbourList = getThreeNeighbourList(x, y, source)

                sobelX = getSobelValue(neighbourList, "X")
                sobelY = getSobelValue(neighbourList, "Y")

                magnitude = Math.Sqrt(Math.Pow(sobelX, 2) + Math.Pow(sobelY, 2))

                If magnitude > 150 Then
                    magnitude = 255
                Else
                    magnitude = 0
                End If

                sobelResult.SetPixel(x, y, Color.FromArgb(magnitude, magnitude, magnitude))
            Next x
        Next y

        PictureBox2.Image = sobelResult
        MessageBox.Show("Proses Sobel Telah Selesai")
    End Sub

    Public Function getSobelValue(ByVal neighbourList As ArrayList, ByVal maskType As String) As Integer
        Dim result As Integer = 0

        Dim sobelX As Integer(,) = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}}
        Dim sobelY As Integer(,) = {{1, 2, 1}, {0, 0, 0}, {-1, -2, -1}}

        Dim count As Integer = 0

        If (maskType.Equals("X")) Then
            For y As Integer = 0 To 2
                For x As Integer = 0 To 2
                    result = result + (sobelX(x, y) * Convert.ToInt16(neighbourList(count)))

                    count = count + 1
                Next x
            Next y
        ElseIf (maskType.Equals("Y")) Then
            For y As Integer = 0 To 2
                For x As Integer = 0 To 2
                    result = result + (sobelY(x, y) * Convert.ToInt16(neighbourList(count)))

                    count = count + 1
                Next x
            Next y
        End If

        Return result
    End Function

    Public Function getThreeNeighbourList(ByVal xPos As Integer, ByVal yPos As Integer, ByVal source As Bitmap) As ArrayList
        Dim neighbourList As ArrayList = New ArrayList

        Dim xStart, xFinish, yStart, yFinish As Integer

        Dim pixel As Integer

        xStart = xPos - 1
        xFinish = xPos + 1

        yStart = yPos - 1
        yFinish = yPos + 1

        For y As Integer = yStart To yFinish
            For x As Integer = xStart To xFinish
                If (x < 0) Or (y < 0) Or (x > source.Width - 1) Or (y > source.Height - 1) Then
                    neighbourList.Add(0)
                Else
                    pixel = source.GetPixel(x, y).R

                    neighbourList.Add(pixel)
                End If
            Next x
        Next y

        Return neighbourList
    End Function
    Private Sub Greyscale()
        Dim source As New Bitmap(PictureBox1.Image)

        Dim red, green, blue, grayscale As Integer

        For y As Integer = 0 To source.Height - 1
            For x As Integer = 0 To source.Width - 1
                red = source.GetPixel(x, y).R
                green = source.GetPixel(x, y).G
                blue = source.GetPixel(x, y).B

                grayscale = red * 0.299 + green * 0.587 + blue * 0.114

                source.SetPixel(x, y, Color.FromArgb(grayscale, grayscale, grayscale))
            Next x
        Next y

        PictureBox1.Image = source
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            SaveFileDialog1.Filter = "JPEG |*.jpg"
            If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                PictureBox2.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
            End If
        Catch ex As Exception
        End Try
    End Sub
End Class

Dan tampilan akhirnya akan tampak seperti ini,



===DONE!===

Visual Basic - Image Processing 4 (greyscale)


Setelah seminggu tidak menulis karena sakit, akhirnya diberi kesempatan kembali untuk menulis lagi, oke pada tutorial sebelumnya kita sudah membuat salah satu sistem dari image processing yaitu cropping, kali ini kita akan membuat sistem greyscale.

Greyscale sendiri adalah warna-warna piksel yang hanya terdiri dari warna hitam dan putih saja.

Belum terlalu mengerti tentang Visual Studio? baca disini, oke langsung saja kita buat enginenya.

Pertama, buat UI (User Interface) pada visual basic menggunakan tools OpenFileDialog, SaveFileDialog, Button, dan PictureBox sehingga tampilannya akan tampak seperti ini,


Selanjutnya, klik dua kali button Open dan ketikkan code berikut,
        OpenFileDialog1.Filter = "image file (*.jpg) |*.jpg| all files (*.*) | *.*"
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If

Kemudian, klik dua kali button Process dan ketikkan code berikut,
PictureBox1.Image = gray(PictureBox1.Image)

Terakhir, klik dua kali button Save dan ketikkan code berikut,
        Try
            SaveFileDialog1.Filter = "JPEG |*.jpeg"
            If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
            End If
        Catch ex As Exception
        End Try

Tambahkan Import dan juga function grey, sehingga fullcodenya akan tampak seperti ini
Imports System.Drawing.Imaging
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "image file (*.jpg) |*.jpg| all files (*.*) | *.*"
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        PictureBox1.Image = gray(PictureBox1.Image)
    End Sub

    Public Function gray(ByVal image As Bitmap) As Bitmap
        Dim a As New Bitmap(image.Width, image.Height)
        Dim b As Graphics = Graphics.FromImage(a)
        Dim c As New ColorMatrix(New Single()() {New Single() {0.3F, 0.3F, 0.3F, 0, 0},
                                                 New Single() {0.59F, 0.59F, 0.59F, 0, 0},
                                                 New Single() {0.11F, 0.11F, 0.11F, 0, 0},
                                                 New Single() {0, 0, 0, 1, 0},
                                                 New Single() {0, 0, 0, 0, 1}})
        Dim d As New ImageAttributes()
        d.SetColorMatrix(c)
        b.DrawImage(image, New Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, d)
        d.Dispose()
        Return a
    End Function

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            SaveFileDialog1.Filter = "JPEG |*.jpeg"
            If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                PictureBox1.Image.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
            End If
        Catch ex As Exception
        End Try
    End Sub
End Class

Dan tampilannya akan tampak seperti ini,


===DONE!===

Visual Basic - Image Processing 3 (cropping)


Tutorial kemarin kita sudah membuat sebuah program yang dapat membaca nilai atau value RGB dan mengkoversinya menjadi sebuah image. Pada tutorial kali ini kita akan membuat sebuah program cropping atau pemotongan gambar.

Baru belajar Visual Basic? pahami artikel ini dulu,
Oke, langsung saja kita mulai tutorialnya,

Pertama, buat UI (User Interface) pada visual basic menggunakan tools OpenFileDialog dan SaveFileDialog  sehingga tampilannya akan tampak seperti ini,


Selanjutnya klik2 kali form UI-nya kemudian ketikkan codenya sehingga akan tampak seperti ini,
Public Class Form1
    Dim selection As Rectangle
    Dim down As Boolean
    Dim filedialog As New OpenFileDialog
    Dim imagebitmap As Bitmap
    Dim selectioncolour As Color = Color.Black
    Dim bordersize As Integer = 2

    Function changealpha(ByVal colour As Color, ByVal value As Integer) As Color
        Dim c As Color
        c = (Color.FromArgb((value), (colour)))
        Return (c)
    End Function
    Function cropimage(ByVal image As Bitmap) As Bitmap
        Dim cropped As Bitmap = image.Clone(selection, image.PixelFormat)
        Return cropped
    End Function

    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.O
                If Not filedialog.ShowDialog = Windows.Forms.DialogResult.Cancel Then imagebitmap = New Bitmap(filedialog.FileName)
                Me.BackgroundImage = imagebitmap
                Me.BackgroundImageLayout = ImageLayout.None
                Me.Invalidate()
            Case Keys.C
                'cropimage(imagebitmap).Save("C:\Data\")
                Try
                    SaveFileDialog1.Filter = "JPEG |*.jpeg"
                    If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        cropimage(imagebitmap).Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
                    End If
                Catch ex As Exception
                End Try
            Case Keys.Space
                selection = New Rectangle(New Point(selection.X, selection.Y), New Size(InputBox("width", "", selection.Width), InputBox("height", "", selection.Height)))
                Me.Invalidate()
        End Select
    End Sub


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
        selection = New Rectangle(New Point(0, 0), New Size(100, 100))
        Me.Invalidate()
    End Sub
    Private Sub Form1_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
        down = True
    End Sub

    Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        If down = True Then
            selection = New Rectangle(e.Location, selection.Size)
            Me.Invalidate()
        End If
    End Sub

    Private Sub Form1_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        down = False
    End Sub

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        e.Graphics.FillRectangle((New SolidBrush((changealpha((selectioncolour), 100)))), selection)
        e.Graphics.DrawRectangle((New Pen((New SolidBrush((selectioncolour))), bordersize)), selection)
        e.Graphics.DrawString(selection.Width & "x" & selection.Height, New Font("arial", 10, FontStyle.Regular), Brushes.White, selection.X, selection.Y)
    End Sub
End Class

Sehingga tampilannya akan tampak seperti ini,


Untuk menggunakannya:
o     : Open file
c     : Crop+save
space : Ubah ukuran crop
mouse : Untuk memindahkan kotak cropping

===DONE!===

Visual Basic - Image Processing 2 (make image from RGB)


Pada tutorial kemarin kita sudah membuat sebuah engine untuk membaca nilai RGB dari suatu Image, sebaliknya tutorial kali ini kita akan membuat sebuah engine yang dapat mengkoversi nilai RGB menjadi sebuah image.

Sebelum memulai tutorial kali ini, ada baiknya pahami terlebih dahulu tutorial sebelumnya.

Oke, jika anda sudah memahami tutorial sebelumnya, saatnya kita memulai prakteknya,

Pertama, buat design UI-nya dengan menggunakan tools richtextbox, button, picturebox, dan groupbox kira-kira tampilannya seperti ini,


Kemudian, klik 2kali button Make image dan ketikkan code berikut,
        Button1.Text = "waiting"
        Button1.Enabled = False

        CheckForIllegalCrossThreadCalls = False

        Dim width As Integer = CInt(TextBox1.Text.Split(CChar("|"))(1).Split(CChar("*"))(0))
        Dim height As Integer = CInt(TextBox1.Text.Split(CChar("|"))(1).Split(CChar("*"))(1))
        bild = New Bitmap(width, height)

        TextBox1.Text = TextBox1.Text.Split(CChar("|"))(0)

        For Each line In TextBox1.Lines

            Dim farbe As String = line.Split(CChar("'"))(0)
            Dim R, G, B, A As Integer
            Dim geteilt() As String = farbe.Split(CChar("-"))

            R = CInt(geteilt(0))
            G = CInt(geteilt(1))
            B = CInt(geteilt(2))
            A = CInt(geteilt(3))

            Dim x As Integer = CInt(CDbl(line.Split(CChar("'"))(1).Split(CChar(","))(0)))
            Dim y As Integer = CInt(CDbl(line.Split(CChar("'"))(1).Split(CChar(","))(1)))

            Dim c As Color = Color.FromArgb(A, R, G, B)
            bild.SetPixel(CInt(x) - 1, CInt(y) - 1, c)
        Next
        PictureBox1.Image = bild
        Button1.Text = "Done"
        Button1.Enabled = True

Selanjutnya tambahkan code berikut untuk mendeklarasikan bild sebagai sebuah object bitmap,
Dim bild As Bitmap

Sehingga fullcodenya akan tampak seperti ini,
Public Class Form1
    Dim bild As Bitmap
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Button1.Text = "waiting"
        Button1.Enabled = False

        CheckForIllegalCrossThreadCalls = False

        Dim width As Integer = CInt(TextBox1.Text.Split(CChar("|"))(1).Split(CChar("*"))(0))
        Dim height As Integer = CInt(TextBox1.Text.Split(CChar("|"))(1).Split(CChar("*"))(1))
        bild = New Bitmap(width, height)

        TextBox1.Text = TextBox1.Text.Split(CChar("|"))(0)

        For Each line In TextBox1.Lines

            Dim farbe As String = line.Split(CChar("'"))(0)
            Dim R, G, B, A As Integer
            Dim geteilt() As String = farbe.Split(CChar("-"))

            R = CInt(geteilt(0))
            G = CInt(geteilt(1))
            B = CInt(geteilt(2))
            A = CInt(geteilt(3))

            Dim x As Integer = CInt(CDbl(line.Split(CChar("'"))(1).Split(CChar(","))(0)))
            Dim y As Integer = CInt(CDbl(line.Split(CChar("'"))(1).Split(CChar(","))(1)))

            Dim c As Color = Color.FromArgb(A, R, G, B)
            bild.SetPixel(CInt(x) - 1, CInt(y) - 1, c)
        Next
        PictureBox1.Image = bild
        Button1.Text = "Done"
        Button1.Enabled = True
    End Sub
End Class

Dan tampilan akhirnya akan tampak seperti ini,



Untuk test, coba silahkan masukkan nilai RGB ini pada view richtextbox-nya,
download disini,

===DONE!===

Visual Basic - Image Processing (read image)


Hello everyone, welcome back,,

Pada tutorial sebelumya kita sudah membahas tentang sistem CRUD pada Visual Basic, tutorial kali ini kita akan memulai sebuah project yang bisa dianggap sebagai project yang sangat spektakuler, dan manghabiskan waktu yang cukup lama bagi codedoct untuk melakukan research pada project ini.

Project yang akan segera dibahas codedoct kali ini adalah Image Processing (Pengolahan Citra) dan Artificial Neural Network(Jaringan Saraf Tiruan).

Jika sama sekali belum mengenal tentang Visual Basic diharapkan untuk memahaminya terlebih dahulu disini.

Oke untuk tutorial yang pertama adalah bagaimana caranya membuat sebuah software atau engine yang mampu membaca nilai RGB (Red Green Blue) dari sebuah citra Image atau gambar menggunakan Visual Basic,

Pertama buat sebuah UI yang terdiri dari picturebox, openfiledialog, textbox, label, dan richtextbox seperti ini,


Kemudian, klik 2 kali button Load Image dan masukkan code berikut, 
OpenFileDialog1.Filter = "image file (*.jpg) |*.jpg| all files (*.*) | *.*"
If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
  PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If

Selanjutnya klik 2 kali button Read Image dan ketikkan code berikut,
Button2.Text = "Please Wait..."
        Button2.Enabled = False

        CheckForIllegalCrossThreadCalls = False
        Dim bild As Bitmap = CType(Me.PictureBox1.Image.Clone, Bitmap)
        Dim width As Integer = bild.Width - 1
        Dim height As Integer = bild.Height - 1
        Dim h, i, j As Integer

        For x = 1 To width
            For y = 1 To height

                Dim R, G, B, A As Integer
                R = bild.GetPixel(x, y).R
                G = bild.GetPixel(x, y).G
                B = bild.GetPixel(x, y).B
                A = bild.GetPixel(x, y).A
                h = h + R
                i = i + G
                j = j + B

                RichTextBox1.AppendText(R & "-" & G & "-" & B & "-" & A & "'" & x & "," & y & vbNewLine)
            Next
        Next
        RichTextBox1.Text = RichTextBox1.Text.Substring(0, RichTextBox1.Text.Length - 1)
        RichTextBox1.AppendText("|" & width & "*" & height)
        hasil1.Text = h / 1000
        hasil2.Text = i / 1000
        hasil3.Text = j / 1000

        Button2.Text = "Load Data"
        Button2.Enabled = True

Sehingga full codenya akan tampak seperti ini,
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        OpenFileDialog1.Filter = "image file (*.jpg) |*.jpg| all files (*.*) | *.*"
        If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Button2.Text = "Please Wait..."
        Button2.Enabled = False

        CheckForIllegalCrossThreadCalls = False
        Dim bild As Bitmap = CType(Me.PictureBox1.Image.Clone, Bitmap)
        Dim width As Integer = bild.Width - 1
        Dim height As Integer = bild.Height - 1
        Dim h, i, j As Integer

        For x = 1 To width
            For y = 1 To height

                Dim R, G, B, A As Integer
                R = bild.GetPixel(x, y).R
                G = bild.GetPixel(x, y).G
                B = bild.GetPixel(x, y).B
                A = bild.GetPixel(x, y).A
                h = h + R
                i = i + G
                j = j + B

                RichTextBox1.AppendText(R & "-" & G & "-" & B & "-" & A & "'" & x & "," & y & vbNewLine)
            Next
        Next
        RichTextBox1.Text = RichTextBox1.Text.Substring(0, RichTextBox1.Text.Length - 1)
        RichTextBox1.AppendText("|" & width & "*" & height)
        hasil1.Text = h / 1000
        hasil2.Text = i / 1000
        hasil3.Text = j / 1000

        Button2.Text = "Load Data"
        Button2.Enabled = True
    End Sub
End Class

Dan tampilannya akan tampak seperti ini,


Masing - masing nilai RGB yang didapat adalah total dari nilai RGB setiap pixel,

===DONE!===

Visual Basic - Membuat sistem (CRUD) Update dan Delete


Melanjutkan tutorial sebelumnya, tutorial kali ini kita akan melengkapi project kita sebelumnya dengan sistem CRUD yang lebih sempurna,

Pada tutorial kali ini kita akan melengkapi project sebelumnya dengan navigasi yang digunakan untuk mengupdate dan mendelete field database.

Oke langsung saja kita praktekan tutorialnya,

Pertama, tambahkan beberapa button baru sehingga tampak seperti ini,


Selanjutnya klik 2x button Prev dan isikan code berikut,
DataUserBindingSource.MovePrevious()

Kemudian klik 2x button Next dan isikan code berikut,
DataUserBindingSource.MoveNext()

Setelah itu klik 2x button Delete dan isikan code berikut,
DataUserBindingSource.RemoveCurrent()

Terakhir klik 2x button Close dan isikan code berikut,
Me.Close()

Full codenya terlihat seperti ini,
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Project_CRUDDataSet1.DataUser' table. You can move, or remove it, as needed.
        Me.DataUserTableAdapter.Fill(Me.Project_CRUDDataSet1.DataUser)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DataUserBindingSource.AddNew()
    End Sub

    Private Sub save_Click(sender As Object, e As EventArgs) Handles save.Click
        On Error GoTo SaveErr
        DataUserBindingSource.EndEdit()
        DataUserTableAdapter.Update(Project_CRUDDataSet1.DataUser)
        MessageBox.Show("Data was saved")
SaveErr:
        Exit Sub
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        DataUserBindingSource.MovePrevious()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        DataUserBindingSource.MoveNext()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        DataUserBindingSource.RemoveCurrent()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Me.Close()
    End Sub
End Class

Saat di "Run" tampilannya akan tampak seperti ini,



===DONE!===

Visual Basic - Membuat sistem (CRUD) Create dan Read


Pada tutorial sebelumnya kita sudah membuat sebuat koneksi database pada project visual studio kita, tutorial kali ini akan mempraktekkan cara membuat operation database untuk "create new data",

Oke langsung saja kita mulai,

Pertama, buka kembali file project kita sebelumnya, dengan tampilan seperti ini,


Klik dua kali button New dan ketikan code berikut,
DataUserBindingSource.AddNew()
Perhatikan pada code diatas code DataUserBindingSource merupakan nama database yang dibuat sehingga sesuaikan dengan nama database yang anda buat, kita bisa melihat nama bindingnya pada tanda kuning gambar diatas,,

Kemudian klik dua kali button Save dan ketikkan code berikut,
On Error GoTo SaveErr
        DataUserBindingSource.EndEdit()
        DataUserTableAdapter.Update(Project_CRUDDataSet.DataUser)
        MessageBox.Show("Data was saved")
SaveErr:
        Exit Sub
Sesuaikan Dataset, Binding, dan TableAdapter sesuai dengan yang anda miliki dapat dilihat pada tanda kuning pada gambar diatas,,

Sehingga tampilan keseluruhan code akan tampak seperti ini,
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Project_CRUDDataSet.DataUser' table. You can move, or remove it, as needed.
        Me.DataUserTableAdapter.Fill(Me.Project_CRUDDataSet.DataUser)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DataUserBindingSource.AddNew()
    End Sub

    Private Sub save_Click(sender As Object, e As EventArgs) Handles save.Click
        On Error GoTo SaveErr
        DataUserBindingSource.EndEdit()
        DataUserTableAdapter.Update(Project_CRUDDataSet.DataUser)
        MessageBox.Show("Data was saved")
SaveErr:
        Exit Sub
    End Sub
End Class

Dan tampilanya akan tampak seperti ini jika di "run"


===DONE!===

Visual Basic - Koneksi database


Hello everyone, senang bisa kembali coding visual basic,,

Kemarin kita sudah membuat sebuah operation file pada file txt, tutorial kali ini akan mempraktekkan cara membuat sebuah koneksi database pada Visual Studio 2013 dengan tipe database Microsoft Access.

Ada banyak tipe database yang bisa digunakan pada Visual Basic tapi untuk kali ini kita akan menggunakan Microsoft Access.

Oke, langsung saja kita mulai tutorialnya.

Pertama buat file Access terlebih dahulu seperti ini.
Pilih blank database dan create


Kemudian akan muncul halaman database anda yang baru dibuat, klik kanan pada table1 dan pilih Design View masukkan nama tabel dan ok.



Selanjutnya masukan nama kolom dan tipe seperti pada gambar berikut.


Dan terakhir save.
Kita sudah selesai membuat file database yang akan kita gunakan pada visual studio, Selanjutnya buat project visual studio baru dan masukkan file database yang baru dibuat tadi satu folder bersama folder project visual studio kita (jika tidak mengerti silahkan pelajari tutorial sebelumnya pada submenu visual studio).

Buat design form visual studio seperti ini.


Untuk membuat GroupBox2 gunakan tool DataGridView,


Dan tampilannya akan tampak seperti ini, jika database belum terkoneksi maka tampilannya akan blank seperti ini.


Klik tombol berwarna kuning dan pilih Add Project Data Source,


Kemudian akan muncul tampilan seperti ini,


Tinggal next saja kemudian pilih Dataset dan next lagi setelah itu akan muncul tampilan berikut,


Data source pilih microsoft access database, kemudian browse database yang baru kita buat tadi dan klik Test Connection, jika error karena database tidak support, maka buka kembali file database access dan save as sebagai file access 2002-2003, coba lagi test connection muncul tampilan berikut,


Klik ok dan klik next kemudian pilih no saja,


Klik next lagi, centang tables dan view dan klik finish. Sehingga tampilan DataGridView akan tampak seperti table yang sudah kita buat pada microsoft access,
Selanjutnya Sinkronisasikan tiap kolomnya dengan form yang ada, caranya klik salah satu text box misalkan text box address kemudian pada properties databindings masukan parameter text sesuai dengan nama kolomnya(misal form address maka databindingsnya adalah kolom address) seperti ini,



Sehingga akan seperti ini hasilnya,


Untuk mengedit tampilan pada DataGridView klik kembali tanda kuning berikut,


Dan klik Edit Columns seperti berikut, buat kolom ID tidak terlihat,


Sekarang "run" dan tampilanya akan tampak seperti ini,


===DONE!===

Visual Basic - Operation file (.txt)


Halo, Tutorial kali ini kita akan membuat sebuah project tentang operation file.

Dimana kita bisa membuat, membuka, mengedit, dan menyimpan sebuah file .txt,

Tutorial ini merupakan lanjutan dari tutorial sebelumnya,

Tutorial ini sangat penting karena operation file ini merupakan tutorial basic pada Visual Studio, dan untuk menuju ke tutorial berikutnya kita akan membuat sebuah project yang jauh lebih sulit, maka dari itu akan lebih mudah jika kita sudah mengerti terlebih dahulu tutorial basic ini.

Oke langsung saja kita praktekkan tutorialnya,,

Pertama, buat project baru namanya terserah anda kemudian silahkan buat design seperti ini,


Pada gambar diatas jangan lupa juga untuk menambahkan OpenFileDialog yang terdapat pada toolbox seperti ini,


Pada button "Open" isikan code berikut dengan cara klik 2 kali button,
.
.
.
        Dim openFile As StreamReader
        OpenFileDialog2.CheckFileExists = True
        OpenFileDialog2.CheckPathExists = True
        OpenFileDialog2.DefaultExt = "txt"
        OpenFileDialog2.FileName = ""
        OpenFileDialog2.Filter = "test|*.txt"
        OpenFileDialog2.Multiselect = False
        If OpenFileDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then
            openFile = New StreamReader(OpenFileDialog2.FileName, True)
            Label1.Text = openFile.ReadToEnd
            Label1.Visible = True
            RichTextBox1.Visible = False
            openFile.Close()
        End If
.
.
.
Lihat code diatas sesuaikan nama OpenFileDialog nya dalam hal ini saya menggunakan nama OpenFileDialog2

Pada button "Edit" isikan code berikut dengan cara klik 2 kali button,
.
.
.
        Dim openFile As StreamReader
        If File.Exists(OpenFileDialog2.FileName) = False Then
            MessageBox.Show("Open file first")
        Else
            openFile = New StreamReader(OpenFileDialog2.FileName, True)
            RichTextBox1.Text = openFile.ReadToEnd
            Label1.Visible = False
            RichTextBox1.Visible = True
            openFile.Close()
        End If
.
.
.

Pada button "Save" isikan code berikut dengan cara klik 2 kali button,
.
.
.
        If File.Exists(OpenFileDialog2.FileName) = True Then
            If RichTextBox1.Text = "" And RichTextBox1.Visible = True Then
                MessageBox.Show("Cannot save, Text is empty")
            ElseIf RichTextBox1.Visible = False Then
                MessageBox.Show("Please click button Edit first")
            Else
                If Label1.Text = RichTextBox1.Text Then
                    MessageBox.Show("Nothing change")
                Else
                    Dim writeFile As New StreamWriter(OpenFileDialog2.FileName, False)
                    writeFile.Write(RichTextBox1.Text)
                    writeFile.Close()
                    MessageBox.Show("File Was Write")
                End If
            End If
        ElseIf File.Exists(OpenFileDialog2.FileName) = False Then
            MessageBox.Show("File txt not found, Open first")
        End If
.
.
.

Jangan lupa untuk menambahakan code Imports System.IO paling atas seperti ini
Imports System.IO
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
.
.
.

Sehingga tampilannya akan tampak seperti ini.


Silahkan pahami sendiri codenya,

===DONE!===

Visual Basic - Introduction


Hello everyone,,

Kali ini saya akan memulai untuk tutorial baru tentang Visual Basic.
Visual Basic  merupakan sebuah sofware yang dapat digunakan untuk membuat sebuh aplikasi console, windows, web dan lain-lain.
Pertama kali saya belajar Visual Basic adalah pada saat membuat sebuah aplikasi image processing untuk tugas skripsi, hehe..

Menurut saya belajar Visual Basic ini terbilang sangat mudah, karena pada Visual Basic itu sendiri ada banyak sekali fitur yang dapat kita manfaatkan,
Jika Anda pernah belajar Java dan Swift tentu anda akan semakin mudah untuk belajar Visual Basic.

Oke langsung saja kita praktekkan tutorialnya,,

Pada tutorial yang pertama ini kita akan berkenalan terlebih dahulu tentang fitur dari visual studio ini, Visual Basic yang saya gunakan saat ini adalah Visual Studio ultimate 2013.
Website ini tidak menyediakan software atau aplikasi bajakan!. Jika ingin mendownload silahkan download di website lain,

Pertama silahkan buka Visual Studio anda dan buat project baru seperti ini,


Dan langsung save saja



Selanjutnya buka window design anda jika belum terbuka klik disini


Silahkan buat design form seperti ini,


Untuk dapat membuatnya silahkan pergunakan toolbox yang ada disamping kiri window,



Jika sudah selesai, klik 2 kali checkbox1 pada design dan isikan code berikut
.
.
.
        If CheckBox1.Checked = True Then
            TextBox1.Enabled = True
        ElseIf CheckBox1.Checked = False Then
            TextBox1.Enabled = False
        End If
.
.
.
Perhatikan pada code diatas TextBox1 sudah diset propertiesnya enabled = false untuk mengatur properties lihat disini


Selanjutnya klik 2 kali CheckBox2 pada design dan isikan code berikut
.
.
.
        If CheckBox2.Checked = True Then
            TextBox2.Visible = True
        ElseIf CheckBox2.Enabled = False Then
            TextBox2.Visible = False
        End If
.
.
.
Pada code diatas TextBox2 sudah diset visible = false

Terakhir klik 2 kali button1 pada design dan isikan code berikut
.
.
.        
        If (CheckBox1.Checked = True And CheckBox2.Checked = True) Then
            MessageBox.Show("Semua sudah di check")
        Else
            MessageBox.Show("Semua CheckBox Harus Dicheck")
        End If
.
.
.

Tampilan seluruh codenya akan tampak seperti ini,
Public Class Form1
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            TextBox1.Enabled = True
        ElseIf CheckBox1.Checked = False Then
            TextBox1.Enabled = False
        End If
    End Sub

    Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.Checked = True Then
            TextBox2.Visible = True
        ElseIf CheckBox2.Enabled = False Then
            TextBox2.Visible = False
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If (CheckBox1.Checked = True And CheckBox2.Checked = True) Then
            MessageBox.Show("Semua sudah di check")
        Else
            MessageBox.Show("Semua CheckBox Harus Dicheck")
        End If
    End Sub
End Class

Sehingga saat anda Start dengan klik tombol ini,


Tampilan akhirnya akan menjadi seperti ini,



Mudah bukan?,,
Jika ada yang ingin ditanyakan silahkan contact us,

===DONE!===