1. Bahan - bahan toolbox nya ada pada tabel dibawah ini :
2. Tampilan Form
3. Listing Program
Judul Spoiler:
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;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace image_resizer
{
public partial class Form1 : Form
{
Bitmap newB;
Image gambar;
Boolean opened = false;
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
tsize.MaxLength = 3;
tsize.Enabled = false;
trackBar1.Enabled = false;
trackBar2.Enabled = false;
btnResize.Enabled = false;
btnReset.Enabled = false;
btnRotate.Enabled = false;
bsimpan.Enabled = false;
panel1.AutoScroll = true;
}
void bukagambar()
{
DialogResult bukagambar = openFileDialog1.ShowDialog();
if (bukagambar == DialogResult.OK)
{
gambar = Image.FromFile(openFileDialog1.FileName);
newB = new Bitmap(openFileDialog1.FileName);
picture.Image = gambar;
opened = true;
picture.SizeMode = PictureBoxSizeMode.StretchImage;
picture.ImageLocation = openFileDialog1.FileName;
ltinggi.Text = gambar.Height.ToString();
llebar.Text = gambar.Width.ToString();
trackBar1.Value = picture.Size.Width;
trackBar2.Value = picture.Size.Height;
trackBar1.Enabled = true;
trackBar2.Enabled = true;
btnResize.Enabled = true;
btnReset.Enabled = true;
btnRotate.Enabled = true;
bsimpan.Enabled = true;
tsize.Enabled = true;
tsize.Clear();
}
}
void ubahsize()
{
if (tsize.Text != "")
{
int persen = Convert.ToInt32(tsize.Text);
int tinggi = (persen * Convert.ToInt32(ltinggi.Text)) / 100;
int lebar = (persen * Convert.ToInt32(llebar.Text)) / 100;
ltinggi.Text = Convert.ToString(tinggi);
llebar.Text = Convert.ToString(lebar);
trackBar1.Value = lebar;
trackBar2.Value = tinggi;
picture.Size = new Size(picture.Size.Width, trackBar1.Value);
picture.Size = new Size(picture.Size.Height, trackBar2.Value);
}
}
void simpangambar()
{
if (picture.Image != null)
{
int tinggi = Convert.ToInt32(ltinggi.Text);
int lebar = Convert.ToInt32(llebar.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();
simpan.Filter = "Jpeg Format|*.Jpg";
simpan.RestoreDirectory = true;
if (simpan.ShowDialog() != DialogResult.Cancel)
{
this.picture.Image.Save(simpan.FileName);
ukuranbaru.Dispose();
MessageBox.Show("Gambar Berhasil Disimpan", "Info");
}
}
else
{
MessageBox.Show("Gambar Masih Kosong");
}
}
private void Bbuka_Click(object sender, EventArgs e)
{
bukagambar();
}
private void btnResize_Click(object sender, EventArgs e)
{
ubahsize();
}
private void bsimpan_Click(object sender, EventArgs e)
{
simpangambar();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
picture.Size = new Size(trackBar1.Value, picture.Size.Height);
llebar.Text = trackBar1.Value.ToString();
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
picture.Size = new Size(picture.Size.Width, trackBar2.Value);
ltinggi.Text = trackBar2.Value.ToString();
}
private void btnRotate_Click(object sender, EventArgs e)
{
newB.RotateFlip(RotateFlipType.Rotate90FlipXY);
picture.Image = newB;
ltinggi.Text = gambar.Width.ToString();
llebar.Text = gambar.Height.ToString();
trackBar1.Value = picture.Size.Width;
trackBar2.Value = picture.Size.Height;
picture.Size = new Size(picture.Size.Width, trackBar2.Value);
picture.Size = new Size(picture.Size.Height, trackBar1.Value);
}
private void btnReset_Click(object sender, EventArgs e)
{
if (opened)
{
gambar = Image.FromFile(openFileDialog1.FileName);
newB = new Bitmap(openFileDialog1.FileName);
picture.Image = gambar;
opened = true;
picture.SizeMode = PictureBoxSizeMode.StretchImage;
picture.ImageLocation = openFileDialog1.FileName;
ltinggi.Text = gambar.Height.ToString();
llebar.Text = gambar.Width.ToString();
trackBar1.Value = picture.Size.Width;
trackBar2.Value = picture.Size.Height;
int tinggi = Convert.ToInt32(ltinggi.Text);
int lebar = Convert.ToInt32(llebar.Text);
ltinggi.Text = Convert.ToString(tinggi);
llebar.Text = Convert.ToString(lebar);
trackBar1.Value = lebar;
trackBar2.Value = tinggi;
picture.Size = new Size(picture.Size.Width, trackBar1.Value);
picture.Size = new Size(picture.Size.Height, trackBar2.Value);
}
else { }
}
}
}
3. Running Program