Artikel ini lanjutan dari Part 3 dan berisi code lengkap dari latihan yang kita buat.
Imports System.Data.SqlClient
Imports System.Drawing.Graphics
Imports System.Drawing
Imports System.Drawing.Bitmap
Imports System.Net
Imports System.IO
Public Class Form1
Dim img1 As Bitmap
Dim img2 As Bitmap
Dim imgMerge As Bitmap
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Text = "Browse..."
Button2.Text = "Browse..."
Button3.Text = "Combine Images!"
Button4.Text = "Save to File"
Button5.Text = "Save to Database"
PictureBox1.BorderStyle = BorderStyle.FixedSingle
PictureBox2.BorderStyle = BorderStyle.FixedSingle
PictureBox3.BorderStyle = BorderStyle.FixedSingle
TextBox1.ReadOnly = True
TextBox2.ReadOnly = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "*.png|*.png|*.gif|*.gif|*.jpg|*.jpg"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
img1 = New Bitmap(TextBox1.Text)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.Load(TextBox1.Text)
PictureBox1.Refresh()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.Filter = "*.png|*.png|*.gif|*.gif|*.jpg|*.jpg"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
TextBox2.Text = OpenFileDialog1.FileName
img2 = New Bitmap(TextBox2.Text)
PictureBox2.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox2.Load(TextBox2.Text)
PictureBox2.Refresh()
End If
End Sub
Public Function CombineImage(ByVal Image1 As Bitmap, _
ByVal Image2 As Bitmap) As Bitmap
Dim imgResult As New Bitmap(Image1.Width + Image2.Width, _
Math.Max(Image1.Height, Image2.Height))
Dim grhResult As Graphics = Graphics.FromImage(imgResult)
grhResult.DrawImage(Image1, New RectangleF(0, 0, _
Image1.Width, Image1.Height))
grhResult.DrawImage(Image2, Image1.Width, 0, _
Image1.Width, Image1.Height)
grhResult.Dispose()
grhResult = Nothing
Return imgResult
End Function
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
If TextBox1.Text = String.Empty Or TextBox2.Text = String.Empty Then
MsgBox("Gambar sumber belum dipilih!")
Exit Sub
Else
imgMerge = CombineImage(img1, img2)
PictureBox3.Image = imgMerge
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
SaveFileDialog1.Filter = "*.png|*.png|*.gif|*.gif|*.jpg|*.jpg"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
imgMerge.Save(SaveFileDialog1.FileName)
MsgBox("Gambar tersimpan ke file.")
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button5.Click
If imgMerge Is Nothing Then MsgBox("Belum ada gambar.") : Exit Sub
Dim stream As New MemoryStream()
Dim content As Byte()
imgMerge.Save(stream, System.Drawing.Imaging.ImageFormat.Png)
content = stream.ToArray()
Try
Dim cnn As SqlConnection
Dim connectionString As String = "Data Source=MYPC;" & _
"Initial Catalog=LATIHAN;Persist Security Info=True;" & _
"User ID=sa;Password=xxxxx"
cnn = New SqlConnection(connectionString)
cnn.Open()
Dim cmd As New SqlCommand("INSERT INTO tblImage (ImageBin) " & _
"VALUES (@img)", cnn)
cmd.Parameters.AddWithValue("@img", content)
cmd.ExecuteNonQuery()
cnn.Close()
MsgBox("Gambar tersimpan ke database.")
Catch ex As Exception
MsgBox("Error :" & ex.Message)
End Try
End Sub
End Class
Happy Coding ^_^
Click here if you like this article.
0 Comments