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, semoga ratio gambar tetap tersadar, 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