Oke sebelum kita mulai, alangkah baiknya ikuti cara-cara yang saya berikan pada gambar berikut :
![]() |
Contoh gambar ketika program berjalan. |
Form1 = Image Resizer = Form1
Button = Buka Gambar = bt_buka
Button = Simpan Gambar = button2
Label = Tinggi Gambar = Label1
Label = Lebar Gambar = Label2
Label = OUT = lb_tg_gmbr
Label = OUT = lb_lbr_gmbr
Label = Resize Gambar = Label5
Label = % = Label6
PictureBox = - = bt_simpan
TextBox1 = - = t_resize
NB : Tanda strip di atas berarti dikosongkan saja.
Berikut Kodingannya :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Pertemuan4
{
public partial class Form1 : Form
{
private Image gambar;
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
t_resize.MaxLength = 3;
t_resize.Enabled = false;
}
private void bt_buka_Click(object sender, EventArgs e)
{
OpenFileDialog bukaGambar = new OpenFileDialog();
if (bukaGambar.ShowDialog() == DialogResult.OK)
{
this.gambar = Image.FromFile(bukaGambar.FileName);
bt_simpan.SizeMode =
PictureBoxSizeMode.StretchImage;
//menyesuaikan gambar sesuai dengan picturebox
bt_simpan.ImageLocation = bukaGambar.FileName;
//mencari lokasi gambar
lb_tg_gmbr.Text = gambar.Height.ToString();
lb_lbr_gmbr.Text = gambar.Width.ToString();
t_resize.Enabled = true;
t_resize.Clear();
}
}
private void label5_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
int tinggi = Convert.ToInt32(lb_tg_gmbr.Text);
int lebar = Convert.ToInt32(lb_lbr_gmbr.Text);
Bitmap ukuranBaru = new Bitmap(lebar,
tinggi, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Graphics Gbr = Graphics.FromImage(ukuranBaru);
Gbr.CompositingQuality =
System.Drawing.Drawing2D.CompositingQuality.HighSpeed;
Gbr.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
Gbr.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
Gbr.PixelOffsetMode =
System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed;
Rectangle rect = new Rectangle(0, 0, lebar, tinggi);
Gbr.DrawImage(gambar, rect);
SaveFileDialog simpan = new SaveFileDialog();
//listing program untuk penyimpanan
simpan.Filter = "Jpeg Format|*.Jpg";
simpan.RestoreDirectory = true;
if (simpan.ShowDialog() != DialogResult.Cancel)
{
ukuranBaru.Save(simpan.FileName);
ukuranBaru.Dispose();
MessageBox.Show("Gambar Berhasil Disimpan", "Info");
}
}
private void resize_key(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (t_resize.Text != "")
{
int persen = Convert.ToInt32(t_resize.Text);
int tinggi = (persen *
Convert.ToInt32(lb_tg_gmbr.Text)) / 100;
int lebar = (persen *
Convert.ToInt32(lb_lbr_gmbr.Text)) / 100;
lb_tg_gmbr.Text = Convert.ToString(tinggi);
lb_lbr_gmbr.Text = Convert.ToString(lebar);
}
}
}
}
}
Oke itu saja dari saya, kurang lebihnya mohon dimaafkan ...
Terima Kasih .
Sampai jumpa ditutorial selanjutnya...
SELAMAT MENCOBA!!!😎😎😎
Tidak ada komentar:
Posting Komentar