membuat program dengan visual basic untuk meenampilkan gambar

13
Membuat program dengan visual basic untuk meenampilkan gambar, mengkopi gambar, menampilkan invert dari gambar yang terkopi, menampilkan koordinat (x,y) jika double klik dilakukan pada salah satu titik pada gambar, menampilkan informasi RGB dan nilai ambang batas (threshold) berikut ini adah tampilan program : Gambar 1. Tampilan window program Algoritma 1. Membuat command button “browse” untuk menampilkan gambar dari sebuah file direktori. 2. Membuat command button “save invert result ” yang jika diklik akan menyimpan invert gambar yang telah ditampilkan. 3. Membuat command button “invert” yang jika di klik akan menampilkan invert dari gambar yang terkopi. 4. Membuat command button “exit” pada pojok kanan bawah untuk keluar dari program. 5. Membuat command button “save threshold result” untuk menyimpan hasil threeshold. 6. Membuat command button “copy” untuk mengkopi gambar. 7. Membuat command button “reset all” untuk melakukan reset.. 8. Membuat 2 group box untukmenampilkan koordinat (x,y) dan menampilkan color information 9. Membuat open file dialog dan save file dialog.

Upload: oka-made

Post on 12-Nov-2014

229 views

Category:

Documents


13 download

TRANSCRIPT

Page 1: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

Membuat program dengan visual basic untuk meenampilkan gambar, mengkopi gambar, menampilkan invert dari gambar yang terkopi, menampilkan koordinat (x,y) jika double klik dilakukan pada salah satu titik pada gambar, menampilkan informasi RGB dan nilai ambang batas (threshold) berikut ini adah tampilan program :

Gambar 1. Tampilan window program

Algoritma

1. Membuat command button “browse” untuk menampilkan gambar dari sebuah file direktori.2. Membuat command button “save invert result ” yang jika diklik akan menyimpan invert

gambar yang telah ditampilkan.3. Membuat command button “invert” yang jika di klik akan menampilkan invert dari gambar

yang terkopi.4. Membuat command button “exit” pada pojok kanan bawah untuk keluar dari program.5. Membuat command button “save threshold result” untuk menyimpan hasil threeshold.6. Membuat command button “copy” untuk mengkopi gambar.7. Membuat command button “reset all” untuk melakukan reset..8. Membuat 2 group box untukmenampilkan koordinat (x,y) dan menampilkan color

information 9. Membuat open file dialog dan save file dialog.

Berikut ini akan dijelaskan proses pembuatan program dengan visual basic 2010. Dibawah ini adalah koding programnya:

Public Class Form1 Dim PicBoxHeight As Integer Dim PicBoxWidth As Integer Dim ImageHeight As Integer Dim ImageWidth As Integer Dim TempImage As Image Dim Image1 As Bitmap Dim Image2 As Bitmap Dim Image3 As Bitmap

Page 2: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

Dim ImageOri As Bitmap Dim scale_factor As Single Dim ColArray(,) As Color

Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click If PictureBox1.Image Is Nothing Then PictureBox1.Enabled = False Else PictureBox1.Enabled = True End If Try PictureBox1.Height = 535 PictureBox1.Width = 354 OpenFileDialog1.Title = "Open Picture" OpenFileDialog1.FileName = "" OpenFileDialog1.Filter = "Format Gambar|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff;*.gif" OpenFileDialog1.ShowDialog() PictureBox1.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName) Label7.Text = "Resolution : " & PictureBox1.Image.Width & "x" & PictureBox1.Image.Height TextBox1.Text = OpenFileDialog1.FileName PictureBox1.Enabled = True ImageOri = New Bitmap(PictureBox1.Image) 'Proses Skala ulang gambar untuk menyesuaikan PictureBox PicBoxHeight = PictureBox1.Height PicBoxWidth = PictureBox1.Width TempImage = PictureBox1.Image ImageHeight = TempImage.Height ImageWidth = TempImage.Width If ImageHeight > PicBoxHeight Then scale_factor = CSng(PicBoxHeight / ImageHeight) End If If (ImageWidth * scale_factor) > PicBoxWidth Then scale_factor = CSng(PicBoxWidth / ImageWidth) End If PictureBox1.Image = TempImage Dim bm_source As New Bitmap(PictureBox1.Image) Dim bm_dest As New Bitmap( _ CInt(bm_source.Width * scale_factor), _ CInt(bm_source.Height * scale_factor)) Dim gr_dest As Graphics = Graphics.FromImage(bm_dest) gr_dest.DrawImage(bm_source, 0, 0, _ bm_dest.Width + 1, _ bm_dest.Height + 1) PictureBox1.Image = bm_dest PictureBox1.Height = PictureBox1.Image.Height PictureBox1.Width = PictureBox1.Image.Width PictureBox2.Image = Nothing PictureBox3.Image = Nothing Image1 = New Bitmap(PictureBox1.Image) Image2 = New Bitmap(PictureBox1.Image) Image3 = New Bitmap(PictureBox1.Image) ReDim ColArray(PictureBox1.Image.Width - 1, PictureBox1.Image.Height - 1) ProgressBar1.Visible = True ProgressBar1.Maximum = (PictureBox1.Image.Width - 1) * (PictureBox1.Image.Height - 1) ProgressBar1.Step = 1 For x = 0 To PictureBox1.Image.Width - 1

Page 3: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

For y = 0 To PictureBox1.Image.Height - 1 ColArray(x, y) = Image1.GetPixel(x, y) ProgressBar1.PerformStep() Next y Next x ProgressBar1.Visible = False ProgressBar1.Value = 0 Catch ex As Exception If PictureBox1.Image Is Nothing Then PictureBox1.Enabled = False Else PictureBox1.Height = PictureBox1.Image.Height PictureBox1.Width = PictureBox1.Image.Width End If End Try End Sub Private Sub cmdCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCopy.Click

If PictureBox1.Image Is Nothing Then MessageBox.Show("There is no image to copy") Else ProgressBar1.Visible = True ProgressBar1.Maximum = (PictureBox1.Image.Width - 1) * (PictureBox1.Image.Height - 1) ProgressBar1.Step = 1 Try Dim x, y As Integer For x = 0 To Image1.Width - 1 For y = 0 To Image1.Height - 1 Image1.SetPixel(x, y, ColArray(x, y)) ProgressBar1.PerformStep() Next y Next x ProgressBar1.Visible = False ProgressBar1.Value = 0 PictureBox2.Image = Image1 Catch ex As ArgumentException End Try End If End Sub

Private Sub cmdInvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInvert.Click If PictureBox1.Image Is Nothing Then MessageBox.Show("There is no image to invert") Else Try Dim x, y As Integer Dim pixcolor As Color Dim newcolor As Color ProgressBar1.Visible = True ProgressBar1.Maximum = (PictureBox1.Image.Width - 1) * (PictureBox1.Image.Height - 1) ProgressBar1.Step = 1 For x = 0 To Image2.Width - 1 For y = 0 To Image2.Height - 1 pixcolor = Image2.GetPixel(x, y) newcolor = Color.FromArgb(Color.White.R - pixcolor.R, Color.White.G - pixcolor.G, Color.White.B - pixcolor.B) Image2.SetPixel(x, y, newcolor) ProgressBar1.PerformStep()

Page 4: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

Next y Next x ProgressBar1.Visible = False ProgressBar1.Value = 0 PictureBox3.Image = Image2 Catch ex As ArgumentException End Try End If End Sub

Private Sub PictureBox1_DoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.DoubleClick Dim TempBitmap As New Bitmap(PictureBox1.Image) Dim MyColor As Color = TempBitmap.GetPixel(e.X, e.Y) LabelR.Text = "R : " & MyColor.R.ToString LabelG.Text = "G :" & MyColor.G.ToString LabelB.Text = "B :" & MyColor.B.ToString LabelX.Text = "X :" & e.X LabelY.Text = "Y :" & e.Y PictureBox4.BackColor = Color.FromArgb(MyColor.R, MyColor.G, MyColor.B) End Sub Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown End Sub Private Sub ButExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click If (MsgBox("Exit Program?", vbYesNo) = vbYes) Then Close() End If End Sub Private Sub cmdReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReset.Click If (PictureBox1.Image Is Nothing And PictureBox2.Image Is Nothing And PictureBox3.Image Is Nothing) Then MessageBox.Show("No image opened") Else If (MsgBox("Reset all?", vbYesNo) = vbYes) Then PictureBox1.Image = Nothing PictureBox1.Enabled = False PictureBox2.Image = Nothing PictureBox3.Image = Nothing PictureBox5.Image = Nothing LabelR.Text = "R :" LabelG.Text = "G :" LabelB.Text = "B :" LabelX.Text = "X :" LabelY.Text = "Y :" Label7.Text = "Resolution :" TextBox1.Text = "" PictureBox1.BackColor = Color.White End If End If End Sub Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click If PictureBox1.Image Is Nothing Then MessageBox.Show("Open an Image First") ElseIf PictureBox3.Image Is Nothing Then MessageBox.Show("Invert an Image") Else

Page 5: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

Try SaveFileDialog1.Title = "Save Picture" SaveFileDialog1.FileName = "" SaveFileDialog1.Filter = "Format Gambar|*.jpg;" SaveFileDialog1.ShowDialog() If SaveFileDialog1.FileName = "" Then Exit Sub Else Dim x, y As Integer ProgressBar1.Visible = True ProgressBar1.Maximum = (ImageOri.Width - 1) * (ImageOri.Height - 1) ProgressBar1.Step = 1 For x = 0 To ImageOri.Width - 1 For y = 0 To ImageOri.Height - 1 Dim pixcolor As Color = ImageOri.GetPixel(x, y) Dim newcolor As Color = _ Color.FromArgb(Color.White.R - pixcolor.R, Color.White.G - pixcolor.G, Color.White.B - pixcolor.B) ImageOri.SetPixel(x, y, newcolor) ProgressBar1.PerformStep() Next y Next x ProgressBar1.Visible = False ProgressBar1.Value = 0 ImageOri.Save(SaveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg) MessageBox.Show("Saved Successfully to" & SaveFileDialog1.FileName) End If Catch ex As Exception End Try End If End Sub Private Sub cmdThres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThres.Click If PictureBox1.Image Is Nothing Then MsgBox("Tidak Ada Gambar Dibuka") Else Dim Tres As Double Dim a As Double Dim imageTres As New Bitmap(Image3) ProgressBar1.Visible = True ProgressBar1.Maximum = (Image3.Width - 1) * (Image3.Height - 1) ProgressBar1.Step = 1 If RadioSkala.Checked = True Then If Not Double.TryParse(TextBox2.Text, a) OrElse a < 0 OrElse a > 1 Then MessageBox.Show("Inputkan Nilai dari 0-1") Else a = TextBox2.Text Tres = a * 255 End If ElseIf RadioAngka.Checked = True Then If Not Double.TryParse(TextBox3.Text, a) Then MessageBox.Show("Inputkan Nilai Berupa Angka") Else Tres = TextBox3.Text End If End If Dim RGB, r, g, b, x, y As Integer For x = 0 To Image3.Width - 1

Page 6: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

For y = 0 To Image3.Height - 1 r = Image3.GetPixel(x, y).R g = Image3.GetPixel(x, y).G b = Image3.GetPixel(x, y).B RGB = (r + g + b) / 3 If RGB < Tres Then imageTres.SetPixel(x, y, Color.Black) Else imageTres.SetPixel(x, y, Color.White) End If ProgressBar1.PerformStep() Next y Next x ProgressBar1.Visible = False ProgressBar1.Value = 0 PictureBox5.Image = imageTres End If End Sub

Private Sub RadioSkala_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioSkala.CheckedChanged If RadioSkala.Checked = False Then TextBox2.Enabled = False Else TextBox2.Enabled = True End If End Sub

Private Sub RadioAngka_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioAngka.CheckedChanged If RadioAngka.Checked = False Then TextBox3.Enabled = False Else TextBox3.Enabled = True End If End Sub

End Class

Kode program 1. Kode program untuk menampilkan gambar, mengkopi gambar, menampilkan invert dari gambar yang terkopi, menampilkan koordinat (x,y) jika double klik dilakukan pada salah satu

titik pada gambar, menampilkan informasi RGB dan nilai ambang batas (threashold)

Berikut ini adalah tampilan programnya ketika dilakukan browsing image

Page 7: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

Gambar 1. Browsing gambar ketika program sedang berjalan.

Ketika user mengklik command button copy akan tampil seperti berikut:

Gambar 2. Gambar yang ditampilkan menggunakan command button “copy”

Ketika user mengklik command button “invert” maka gambar akan menjadi negatif, dibawah ini adalah tampilan window programnya:

Page 8: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

Gambar 3. Invert gambar yang ditampilkan menggunakan command button “invert”

Ketika user mengklik gambar maka akan tampil informasi koordinat (x,y) dan nilai RGB pada titik yang diklik, dibawah ini adalah tampilan window programnya:

Gambar 4. Informasi RGB dan koordinat (x,y) gambar yang ditampilkan ketika user melakukan double klik.

Ketika user memasukan nilai threshold skala (range 0-1) maka akan tampil gambar biner hasil threshold, dibawah ini adalah tampilannya:

Page 9: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

Gambar 5. Gambar hasil threshold dengan skala.

Jika user memasukkan nilai diluar range (0-1). Maka akan muncul messages box “inputkan nilai dari 0-1” berikut ini adalah tampilannya.

Gambar 6. Messages box inputkan nilai dari 0-1.

Jika user memasukkan angka pada group box threshold maka akan muncul gambar hasilnya. Berikut adalah tampilannya:

Page 10: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

Gambar 7. Gambar hasil threshold dengan angka.

Page 11: Membuat Program Dengan Visual Basic Untuk Meenampilkan Gambar

LAPORAN TUGAS PENGOLAHAN CITRA DIGITAL

Oleh :

IDA BAGUS WIDNYANA PRAWITRA (1104505016)

JURUSAN TEKNOLOGI INFORMASI FAKULTAS TEKNIKUNIVERSITAS UDAYANA

BUKIT JIMBARAN12 Oktober September 2012