Membuat Photo Editor menggunakan visual basic 2008/VB.net

Membuat Photo Editor menggunakan visual basic 2008/VB.net (tahap I)


Untuk Membuat Photo Editor menggunakan visual basic 2008/VB.net cukup rumit, oleh karena itu saya akan membuat tutorialnya bertahap, pada tahap awal ini saya akan menerangkan bagaimana memembuat brightness dan contrass pada gambar dengan menggunakan Visual Basic 2008, langkah awal untuk membuat Brightness dan contrast pada gambar adalah dengan membuat 1 form dan 1 class (masukkan nama filters pada saat membuat class).
Pada Form1 masukkan komponen-komponen dibawah:
  • 1 picturebox
  • 2 trackbar
  • 2 label
  • 1 button
  • 1 openfiledialog
 Letakkan Picturebox dibagian atas, kemudian letakkan button1 dibawah picturebox untuk mempersingkat tulisan lihat saja gambar dibawah ini:

Kemudian klik 2 kali Class yang berada pada list sebelah kanan (Slutions Explorer) hapus semua kode dan masukkan kode dibawah ini:
Imports System.Runtime.InteropServices ' The Marshal class is derieved from here
Imports System.Drawing.Imaging         ' BitmapData structure from here

Public Class Filters

    Shared bmData As BitmapData
    Shared ptr As System.IntPtr
    Shared Red As Integer, Green As Integer, Blue As Integer
    Shared x As Integer, y As Integer
    Shared nOffset As Integer

    Public Shared Function ContrastFilter(ByVal ContrastValue As Integer, ByRef b As Bitmap) As Bitmap
        If b.PixelFormat = PixelFormat.Format8bppIndexed Then
            MsgBox("Tidak support dengan gambar value warna 256.", MsgBoxStyle.Critical Or MsgBoxStyle.ApplicationModal, "Error")
            Return Nothing
        End If
        If (ContrastValue < -100 Or ContrastValue > 100) Then Return Nothing

        bmData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
                ptr = bmData.Scan0
        nOffset = bmData.Stride - b.Width * 3
        Dim pixel As Double
        Dim contrast As Double = (100 + ContrastValue) / 100
        contrast *= contrast
                For y = 0 To b.Height - 1
            For x = 0 To b.Width - 1
                Blue = Marshal.ReadByte(ptr, 0)
                pixel = Blue / 255
                pixel -= 0.5
                pixel *= contrast
                pixel += 0.5
                pixel *= 255
                If (pixel < 0) Then pixel = 0
                If (pixel > 255) Then pixel = 255
                Marshal.WriteByte(ptr, 0, CByte(pixel))

                Green = Marshal.ReadByte(ptr, 1)
                pixel = Green / 255
                pixel -= 0.5
                pixel *= contrast
                pixel += 0.5
                pixel *= 255
                If (pixel < 0) Then pixel = 0
                If (pixel > 255) Then pixel = 255
                Marshal.WriteByte(ptr, 1, CByte(pixel))

                Red = Marshal.ReadByte(ptr, 2)
                pixel = Red / 255
                pixel -= 0.5
                pixel *= contrast
                pixel += 0.5
                pixel *= 255
                If (pixel < 0) Then pixel = 0
                If (pixel > 255) Then pixel = 255
                Marshal.WriteByte(ptr, 2, CByte(pixel))
                ptr = IntPtr.op_Explicit(ptr.ToInt32 + 3)
            Next
            ptr = IntPtr.op_Explicit(ptr.ToInt32 + nOffset)
        Next
        b.UnlockBits(bmData)
        Return b
    End Function

    Public Shared Function BrightnessFilter(ByVal BrightnessValue As Integer, ByRef b As Bitmap) As Bitmap
        If b.PixelFormat = PixelFormat.Format8bppIndexed Then
            MsgBox("256 colors bitmap are not supported.", MsgBoxStyle.Critical Or MsgBoxStyle.ApplicationModal, "Error")
            Return Nothing
        End If
        If BrightnessValue = 0 Then Return Nothing
        bmData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
        ptr = bmData.Scan0
        nOffset = bmData.Stride - b.Width * 3
        For y = 0 To b.Height - 1
            For x = 0 To (b.Width * 3) - 1
                Dim bByte As Integer = Marshal.ReadByte(ptr, 0)
                bByte += BrightnessValue
                If bByte > 255 Then bByte = 255
                If bByte < 0 Then bByte = 0

                Marshal.WriteByte(ptr, 0, CByte(bByte))
                ptr = IntPtr.op_Explicit(ptr.ToInt32 + 1)
            Next
            ptr = IntPtr.op_Explicit(ptr.ToInt32 + nOffset)
        Next
        b.UnlockBits(bmData)
        Return b
    End Function

    Public Shared Function Invert(ByRef b As Bitmap) As Bitmap
        If b.PixelFormat = PixelFormat.Format8bppIndexed Then
            MsgBox("256 colors bitmap are not supported.", MsgBoxStyle.Critical Or MsgBoxStyle.ApplicationModal, "Error")
            Return Nothing
        End If

        bmData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
        ptr = bmData.Scan0
        nOffset = bmData.Stride - b.Width * 3
        For y = 0 To b.Height - 1
             For x = 0 To (b.Width * 3) - 1
                Marshal.WriteByte(ptr, 0, CByte(255 - Marshal.ReadByte(ptr, 0)))
               
                ptr = IntPtr.op_Explicit(ptr.ToInt32 + 1)
            Next
            ptr = IntPtr.op_Explicit(ptr.ToInt32 + nOffset)
        Next
        b.UnlockBits(bmData)
        Return b
    End Function

    Public Shared Function Grayscale(ByRef b As Bitmap) As Bitmap
        If b.PixelFormat = PixelFormat.Format8bppIndexed Then
            MsgBox("256 colors bitmap are not supported.", MsgBoxStyle.Critical Or MsgBoxStyle.ApplicationModal, "Error")
            Return Nothing
        End If
        bmData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
        ptr = bmData.Scan0
        nOffset = bmData.Stride - b.Width * 3
        Dim bVal As Byte
        For y = 0 To b.Height - 1
            For x = 0 To b.Width - 1
                Blue = Marshal.ReadByte(ptr, 0)
                Green = Marshal.ReadByte(ptr, 1)
                Red = Marshal.ReadByte(ptr, 2)
                bVal = CByte(0.299 * Red + 0.587 * Green + 0.114 * Blue)
                Marshal.WriteByte(ptr, 0, bVal)
                Marshal.WriteByte(ptr, 1, bVal)
                Marshal.WriteByte(ptr, 2, bVal)
                ptr = IntPtr.op_Explicit(ptr.ToInt32 + 3)
            Next
            ptr = IntPtr.op_Explicit(ptr.ToInt32 + nOffset)
        Next
        b.UnlockBits(bmData)
        Return b
    End Function

    Public Shared Function AdjustColors(ByRef b As Bitmap, ByVal RedValue As Integer, ByVal BlueValue As Integer, ByVal GreenValue As Integer) As Bitmap
        If b.PixelFormat = PixelFormat.Format8bppIndexed Then
            MsgBox("256 colors bitmap are not supported.", MsgBoxStyle.Critical Or MsgBoxStyle.ApplicationModal, "Error")
            Return Nothing
        End If
        bmData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
        ptr = bmData.Scan0
        Dim bVal As Byte
        nOffset = bmData.Stride - b.Width * 3
        For y = 0 To b.Height - 1
            For x = 0 To b.Width - 1
                Blue = Marshal.ReadByte(ptr, 0)
                Green = Marshal.ReadByte(ptr, 1)
                Red = Marshal.ReadByte(ptr, 2)


                Red += RedValue
                Red = Math.Max(Red, 0)
                Red = Math.Min(Red, 255)


                Green += GreenValue
                Green = Math.Max(Green, 0)
                Green = Math.Min(Green, 255)


                Blue += BlueValue
                Blue = Math.Max(Blue, 0)
                Blue = Math.Min(Blue, 255)


                Marshal.WriteByte(ptr, 0, CByte(Blue))
                Marshal.WriteByte(ptr, 1, CByte(Green))
                Marshal.WriteByte(ptr, 2, CByte(Red))
                ptr = IntPtr.op_Explicit(ptr.ToInt32 + 3)
            Next
            ptr = IntPtr.op_Explicit(ptr.ToInt32 + nOffset)
        Next
        b.UnlockBits(bmData)
        Return b
    End Function

    Public Shared Function AdjustGamma(ByRef b As Bitmap, ByVal RedValue As Double, ByVal GreenValue As Double, ByVal BlueValue As Double) As Bitmap



        If b.PixelFormat = PixelFormat.Format8bppIndexed Then
            MsgBox("256 colors bitmap are not supported.", MsgBoxStyle.Critical Or MsgBoxStyle.ApplicationModal, "Error")
            Return Nothing
        End If


        If (RedValue < 0.2 Or RedValue > 5) Then Return Nothing
        If (GreenValue < 0.2 Or GreenValue > 5) Then Return Nothing
        If (BlueValue < 0.2 Or BlueValue > 5) Then Return Nothing

        Dim redGamma(256) As Byte
        Dim greenGamma(256) As Byte
        Dim blueGamma(256) As Byte

        Dim i As Integer

        For i = 0 To 255
            redGamma(i) = CByte(Math.Min(255, CInt(((255.0 * Math.Pow(i / 255.0, 1.0 / RedValue)) + 0.5))))
            greenGamma(i) = CByte(Math.Min(255, CInt(((255.0 * Math.Pow(i / 255.0, 1.0 / GreenValue)) + 0.5))))
            blueGamma(i) = CByte(Math.Min(255, CInt(((255.0 * Math.Pow(i / 255.0, 1.0 / BlueValue)) + 0.5))))
        Next

        bmData = b.LockBits(New Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
        ptr = bmData.Scan0
        nOffset = bmData.Stride - b.Width * 3
        For y = 0 To b.Height - 1
            For x = 0 To b.Width - 1
                Marshal.WriteByte(ptr, 0, blueGamma(Marshal.ReadByte(ptr, 0)))
                Marshal.WriteByte(ptr, 1, greenGamma(Marshal.ReadByte(ptr, 1)))
                Marshal.WriteByte(ptr, 2, redGamma(Marshal.ReadByte(ptr, 2)))
                ptr = IntPtr.op_Explicit(ptr.ToInt32 + 3)
            Next
            ptr = IntPtr.op_Explicit(ptr.ToInt32 + nOffset)
        Next
        b.UnlockBits(bmData)
        Return b
    End Function

End Class
Kemudian Klik 2 kali form1, hapus semua kode yang ada dan masukkan kode dibawah ini:

Public Class Form1
    Dim op As OpenFileDialog
    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        On Error Resume Next


       
        Filters.BrightnessFilter(TrackBar1.Value, CType(PictureBox1.Image, Bitmap))
        PictureBox1.Refresh()
        Cursor.Current = Cursors.Arrow
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TrackBar1.Minimum = -255
        TrackBar1.Maximum = 255
        TrackBar2.Minimum = -255
        TrackBar2.Maximum = 255
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            PictureBox1.ImageLocation = OpenFileDialog1.FileName
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        End If
    End Sub

    Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
        Filters.ContrastFilter(TrackBar2.Value, CType(PictureBox1.Image, Bitmap))
        PictureBox1.Refresh()
        Cursor.Current = Cursors.Arrow
    End Sub
End Class
 
Share this article :

Penulis : Unknown ~ Sebuah blog yang menyediakan berbagai macam informasi

Artikel Membuat Photo Editor menggunakan visual basic 2008/VB.net ini dipublish oleh Unknown pada hari Rabu, 02 Juli 2014. Semoga artikel ini dapat bermanfaat.Terimakasih atas kunjungan Anda silahkan tinggalkan komentar.sudah ada 0 komentar: di postingan Membuat Photo Editor menggunakan visual basic 2008/VB.net
 

0 komentar:

Posting Komentar