Saat membangun sebuah aplikasi tidak jarang kita berurusan dengan penyimpanan gambar. Dan untuk menyimpan gambar secara efektif kadang kita perlu mengubah ukuran supaya lebih kecil. Nah kesempatan ini kita akan membuat contoh bagaimana sebuah file gambar diperkecil dengan menggunakan code VB.NET.
Proses pengubahan ukuran pada contoh kali ini, ukuran akan diubah dengan pilihan:
- Melalui Persentasi
- Melalui Width (lebar gambar) --> height otomatis menyesuaikan
- Melalui Height (tinggi gambar)--> width otomatis menyesuaikan
Mengapa Width dan Height tidak di set keduanya, supaya ratio gambar tetap terjaga, alias ga gepeng gituh...
UI:
Code:
Imports System.Drawing.Imaging
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.BorderStyle = BorderStyle.FixedSingle
Label1.Text = "Image Source"
Label2.Text = "Resize Type"
Label3.Text = "Size to"
Button1.Text = "Browse..."
Button2.Text = "Resize"
With ComboBox1
.Items.Add("Percentage")
.Items.Add("Width Scale")
.Items.Add("Height Scale")
.SelectedIndex = 0
End With
'default value of scale
TextBox2.Text = 50
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedIndex = 0 Then
Label4.Text = "%"
Else
Label4.Text = "px"
End If
End Sub
Public Sub ResizeImage(ByVal ImageSource As String, _
ByVal ResizeType As Integer, ByVal scale As Single, _
ByVal ResultPath As String)
'source bitmap.
Dim imgSource As New Bitmap(ImageSource)
'resize type
Select Case ResizeType
Case 0 'percentage
scale = scale / 100
Case 1 'width px
scale = scale / (imgSource.Width)
Case 2 'height px
scale = scale / (imgSource.Height)
End Select
'scale factor
Dim sclFactor As Single = Single.Parse(scale)
'bitmap for result
Dim imgResult As New Bitmap( _
CInt(imgSource.Width * sclFactor), _
CInt(imgSource.Height * sclFactor))
'Graphics object for result
Dim grResult As Graphics = Graphics.FromImage(imgResult)
'source to result bitmap
grResult.DrawImage(imgSource, 0, 0, _
imgResult.Width + 1, _
imgResult.Height + 1)
'save result
imgResult.Save(ResultPath, ImageFormat.Jpeg)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
With OpenFileDialog1
.FileName = String.Empty
.InitialDirectory = "C:\"
.Title = "Open Image File"
'filter image file
.Filter = "Image Files |*.jpg;*.jpeg;*.png;*.bmp"
End With
Dim result As DialogResult = OpenFileDialog1.ShowDialog()
If result = Windows.Forms.DialogResult.OK Then
Try
TextBox1.Text = OpenFileDialog1.FileName
PictureBox1.ImageLocation = OpenFileDialog1.FileName
Catch ex As Exception
MsgBox("Error : " & ex.Message)
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
SaveFileDialog1.Filter = "Image Files |*.jpg;*.jpeg;*.png;*.bmp"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
ResizeImage(TextBox1.Text, ComboBox1.SelectedIndex, _
TextBox2.Text, SaveFileDialog1.FileName)
MsgBox("Image resized.")
End If
End Sub
End Class
Runtime:
Saat aplikasi pertama berjalan, tekan tombol Browse... untuk memilih file gambar.
Pilih file gambar.
Pilih tipe resize.
Kemudian klik tombol Resize, dan tentukan tempat penyimpanan hasil perubahan ukuran.
Jika berhasil akan muncul message berikut.
Hasil ukuran dan dimensi file.
Click here if you like this article.
0 Comments