Saturday, December 12, 2009

Proyekt I: Inisialisasi dan pengisian event untuk button

Pembahasan kali ini adalah tentang inisialisasi dan pengisian event untuk button jika di pilih oleh pengguna. Inisialisasi dilakukan dengan pengisian nilai awal untuk komponen-komponen seperti combobox dan textbox yang ada. Sedangkan event button diisi dengan rutin-rutin yang akan dijalankan jika button tersebut dipilih oleh pengguna aplikasi.

Inisialisasi untuk combobox dilakukan untuk mengisi dua buah combobox yang dipakai yaitu cbPenghalusan dan cbInterpolasi. Sedangkan Inisialisasi untuk textbox dilakukan untuk txtTujuanLebar dan txtTujuanTinggi. Berikut adalah kode untuk inisialisasi masing-masing komponen tersebut:
    //Inisialisasi
    cbPenghalusan.Items.Clear();
    cbPenghalusan.Items.Add("Default");
    cbPenghalusan.Items.Add("HighSpeed");
    cbPenghalusan.Items.Add("HighQuality");
    cbPenghalusan.SelectedIndex=0;

    cbInterpolasi.Items.Clear();
    cbInterpolasi.Items.Add("Default");
    cbInterpolasi.Items.Add("Bilinear");
    cbInterpolasi.Items.Add("Bicubic");
    cbInterpolasi.Items.Add("High");
    cbInterpolasi.Items.Add("HighQualityBilinear");
    cbInterpolasi.Items.Add("HighQualityBicubic");
    cbInterpolasi.Items.Add("Low");
    cbInterpolasi.Items.Add("NearestNeighbour");
    cbInterpolasi.SelectedIndex = 0;
    
    txtTujuanLebar.Text = "640";
    txtTujuanTinggi.Text = "480";


Pengisian Event untuk button disisi lain dimaksudkan untuk mengisi rutin-rutin yang akan dijalankan jika button tersebut ditekan oleh pengguna. Ada tiga buah button yaitu btnAmbil, btnProses, dan btnSelesai. btnAmbil berisi rutin untuk mengambil file foto yang akan diproses, btnProses untuk melakukan proses terhadap foto tersebut dan menyimpan hasilnya, sedangkan btnSelesai berisi rutin untuk mengakhiri penggunaan aplikasi. Berikut adalah rutin untuk pengisian event tersebut:
    ...
    //Penambahan Aksi Tombol:
    btnAmbil.Click += new System.EventHandler(this.btnAmbil_Click);    
    btnProses.Click += new System.EventHandler(this.btnProses_Click);
    btnSelesai.Click += new System.EventHandler(this.btnSelesai_Click);
    ...

  private void btnAmbil_Click(object sender, EventArgs e)
  {
      OpenFileDialog ofd;
      ofd = new OpenFileDialog();
      ofd.Filter = "JPeg File (*.jpg)|*.jpg||";            
      if (ofd.ShowDialog() == DialogResult.OK )
      {
          String tx = ofd.FileName;
          txtFotoAsal.Text = ofd.FileName;
          tx = tx.Substring(0,ofd.FileName.Length-ofd.SafeFileName.Length);
          txtFotoTujuan.Text = tx + "Dest-" + ofd.SafeFileName;
          System.IO.FileInfo f = new System.IO.FileInfo(ofd.FileName);
          long size = f.Length;                
          Bitmap b = new Bitmap(ofd.FileName);
          txtAsalUkuran.Text = size.ToString("#,##0");
          txtAsalLebar.Text = b.Width.ToString("#,##0");
          txtAsalTinggi.Text = b.Height.ToString("#,##0");
          b.Dispose();                
      }
      
  }

  private void btnSelesai_Click(object sender, EventArgs e)
  {
      Close();
  }

  private void btnProses_Click(object sender, EventArgs e)
  {
      int w;
      int h;

      progressBar1.Value = 10;
      int.TryParse( txtTujuanLebar.Text, out w);
      int.TryParse(txtTujuanTinggi.Text, out h);


      progressBar1.Value = 20;
      Bitmap bmpAsal = new Bitmap(txtFotoAsal.Text);
      Bitmap bmpTujuan = new Bitmap(w, h, bmpAsal.PixelFormat);
      Graphics g = Graphics.FromImage(bmpTujuan);


      progressBar1.Value = 30;
      //Smoothing Mode
      switch (cbPenghalusan.SelectedIndex)
      {
          case 0:
              g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
              break;
          case 1:
              g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
              break;
          case 2:
              g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
              break;
      }

      progressBar1.Value = 40;

      switch (cbInterpolasi.SelectedIndex)
      {
          case 0:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
              break;
          case 1:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
              break;
          case 2:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
              break;
          case 3:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
              break;
          case 4:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
              break;
          case 5:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
              break;
          case 6:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
              break;
          case 7:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
              break;
      }

      progressBar1.Value = 50;
      
      g.DrawImage(bmpAsal, 0, 0, w, h);
      g.Dispose();

      progressBar1.Value = 60;
      System.Drawing.Imaging.ImageCodecInfo[] c = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); ;
      System.Drawing.Imaging.ImageCodecInfo ss = c[0];
      for (int i = 0; i < c.Length; i++)
      {
          if (c[i].MimeType.Equals("image/jpeg"))
          {
              ss =  c[i];
          }
      }
      progressBar1.Value = 70;
      bmpTujuan.Save(txtFotoTujuan.Text, ss, null);

      progressBar1.Value = 80;
      bmpAsal.Dispose();
      bmpTujuan.Dispose();

      progressBar1.Value = 90;

      System.IO.FileInfo fi = new System.IO.FileInfo(txtFotoTujuan.Text);
      txtTujuanUkuran.Text = fi.Length.ToString("#,##0");
      progressBar1.Value = 100;
      
  }  

Hasil akhir dari semua langkah di atas bisa dikumpulkan dalam rutin dibawah ini, simpanlah dengan menggunakan nama file PhotoResizer.cs.
using System;
using System.Windows.Forms;
using System.Drawing;

public class PhotoResizer : Form 
{
  //Deklarasi
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Label label6;
  private System.Windows.Forms.Label label7;
  private System.Windows.Forms.Label label8;
  private System.Windows.Forms.Label label9;
  private System.Windows.Forms.Label label10;

  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.GroupBox groupBox2;
  private System.Windows.Forms.GroupBox groupBox3;
  private System.Windows.Forms.GroupBox groupBox4;

  private System.Windows.Forms.TextBox txtFotoAsal;
  private System.Windows.Forms.TextBox txtFotoTujuan;
  private System.Windows.Forms.Button btnAmbil;
  private System.Windows.Forms.TextBox txtAsalTinggi;
  private System.Windows.Forms.TextBox txtAsalLebar;
  private System.Windows.Forms.TextBox txtAsalUkuran;
  private System.Windows.Forms.TextBox txtTujuanTinggi;
  private System.Windows.Forms.TextBox txtTujuanLebar;
  private System.Windows.Forms.TextBox txtTujuanUkuran;
  private System.Windows.Forms.ComboBox cbInterpolasi;
  private System.Windows.Forms.ComboBox cbPenghalusan;

  private System.Windows.Forms.Button btnSelesai;
  private System.Windows.Forms.Button btnProses;

  private System.Windows.Forms.ProgressBar progressBar1;
  public PhotoResizer()
  {
    InitComponent();
  }
  
  private void InitComponent()
  {
    label1 = new System.Windows.Forms.Label();
    label2 = new System.Windows.Forms.Label();
    label3 = new System.Windows.Forms.Label();
    label4 = new System.Windows.Forms.Label();
    label5 = new System.Windows.Forms.Label();
    label6 = new System.Windows.Forms.Label();
    label7 = new System.Windows.Forms.Label();
    label8 = new System.Windows.Forms.Label();
    label9 = new System.Windows.Forms.Label();
    label10 = new System.Windows.Forms.Label();

    txtFotoAsal = new System.Windows.Forms.TextBox();
    txtFotoTujuan = new System.Windows.Forms.TextBox();
    btnAmbil = new System.Windows.Forms.Button();

    groupBox1 = new System.Windows.Forms.GroupBox();
    txtAsalUkuran = new System.Windows.Forms.TextBox();
    txtAsalLebar = new System.Windows.Forms.TextBox();
    txtAsalTinggi = new System.Windows.Forms.TextBox();

    groupBox2 = new System.Windows.Forms.GroupBox();
    txtTujuanTinggi = new System.Windows.Forms.TextBox();
    txtTujuanLebar = new System.Windows.Forms.TextBox();
    txtTujuanUkuran = new System.Windows.Forms.TextBox();

    groupBox3 = new System.Windows.Forms.GroupBox();
    cbPenghalusan = new System.Windows.Forms.ComboBox();
    cbInterpolasi = new System.Windows.Forms.ComboBox();

    groupBox4 = new System.Windows.Forms.GroupBox();
    progressBar1 = new System.Windows.Forms.ProgressBar();
    btnProses = new System.Windows.Forms.Button();
    btnSelesai = new System.Windows.Forms.Button();
    // 
    // label1
    // 
    label1.Location = new System.Drawing.Point(13, 5);
    label1.Size = new System.Drawing.Size(77, 13);
    label1.TabIndex = 0;
    label1.Text = "File Photo Asal";
    // 
    // label2
    // 
    label2.Location = new System.Drawing.Point(13, 31);
    label2.Size = new System.Drawing.Size(66, 13);
    label2.TabIndex = 0;
    label2.Text = "Disimpan Ke";
    // 
    // txtFotoAsal
    // 
    txtFotoAsal.Location = new System.Drawing.Point(119, 5);
    txtFotoAsal.ReadOnly = true;
    txtFotoAsal.Size = new System.Drawing.Size(341, 20);
    txtFotoAsal.TabIndex = 1;
    // 
    // txtFotoTujuan
    // 
    txtFotoTujuan.Location = new System.Drawing.Point(119, 31);
    txtFotoTujuan.Size = new System.Drawing.Size(425, 20);
    txtFotoTujuan.TabIndex = 1;
    // 
    // btnAmbil
    // 
    btnAmbil.Location = new System.Drawing.Point(469, 5);
    btnAmbil.Size = new System.Drawing.Size(75, 23);
    btnAmbil.TabIndex = 0;
    btnAmbil.Text = "Ambil";
    // 
    // groupBox1
    // 
    groupBox1.Controls.Add(label3);
    groupBox1.Controls.Add(label4);
    groupBox1.Controls.Add(label5);
    groupBox1.Controls.Add(txtAsalUkuran);
    groupBox1.Controls.Add(txtAsalLebar);
    groupBox1.Controls.Add(txtAsalTinggi);
    groupBox1.Location = new System.Drawing.Point(16, 61);
    groupBox1.Size = new System.Drawing.Size(261, 96);
    groupBox1.TabIndex = 3;
    groupBox1.TabStop = false;
    groupBox1.Text = "Informasi File Asal";
    // 
    // label3
    // 
    label3.Location = new System.Drawing.Point(7, 20);
    label3.Size = new System.Drawing.Size(96, 13);
    label3.TabIndex = 0;
    label3.Text = "Ukuran File (Bytes)";
    // 
    // txtAsalUkuran
    // 
    txtAsalUkuran.Location = new System.Drawing.Point(121, 20);
    txtAsalUkuran.ReadOnly = true;
    txtAsalUkuran.Size = new System.Drawing.Size(134, 20);
    txtAsalUkuran.TabIndex = 1;
    txtAsalUkuran.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    // 
    // label4
    // 
    label4.Location = new System.Drawing.Point(7, 46);
    label4.Size = new System.Drawing.Size(95, 13);
    label4.TabIndex = 0;
    label4.Text = "Lebar Foto (Piksel)";
    // 
    // txtAsalLebar
    // 
    txtAsalLebar.Location = new System.Drawing.Point(121, 46);
    txtAsalLebar.ReadOnly = true;
    txtAsalLebar.Size = new System.Drawing.Size(134, 20);
    txtAsalLebar.TabIndex = 1;
    txtAsalLebar.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    // 
    // label5
    // 
    label5.Location = new System.Drawing.Point(7, 70);
    label5.Size = new System.Drawing.Size(97, 13);
    label5.TabIndex = 0;
    label5.Text = "Tinggi Foto (Piksel)";
    // 
    // txtAsalTinggi
    // 
    txtAsalTinggi.Location = new System.Drawing.Point(121, 70);
    txtAsalTinggi.ReadOnly = true;
    txtAsalTinggi.Size = new System.Drawing.Size(134, 20);
    txtAsalTinggi.TabIndex = 1;
    txtAsalTinggi.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    // 
    // groupBox2
    // 
    groupBox2.Controls.Add(label6);
    groupBox2.Controls.Add(label7);
    groupBox2.Controls.Add(label8);
    groupBox2.Controls.Add(txtTujuanUkuran);
    groupBox2.Controls.Add(txtTujuanLebar);
    groupBox2.Controls.Add(txtTujuanTinggi);
    groupBox2.Location = new System.Drawing.Point(283, 61);
    groupBox2.Size = new System.Drawing.Size(261, 96);
    groupBox2.TabIndex = 3;
    groupBox2.TabStop = false;
    groupBox2.Text = "Setting File Tujuan";
    // 
    // label6
    // 
    label6.AutoSize = true;
    label6.Location = new System.Drawing.Point(7, 20);
    label6.Name = "label6";
    label6.Size = new System.Drawing.Size(96, 13);
    label6.TabIndex = 0;
    label6.Text = "Ukuran File (Bytes)";
    // 
    // txtTujuanUkuran
    // 
    txtTujuanUkuran.Location = new System.Drawing.Point(121, 20);
    txtTujuanUkuran.Name = "txtTujuanUkuran";
    txtTujuanUkuran.ReadOnly = true;
    txtTujuanUkuran.Size = new System.Drawing.Size(134, 20);
    txtTujuanUkuran.TabIndex = 1;
    txtTujuanUkuran.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    // 
    // txtTujuanLebar
    // 
    txtTujuanLebar.Location = new System.Drawing.Point(121, 46);
    txtTujuanLebar.Name = "txtTujuanLebar";
    txtTujuanLebar.Size = new System.Drawing.Size(134, 20);
    txtTujuanLebar.TabIndex = 1;
    txtTujuanLebar.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    // 
    // label7
    // 
    label7.Location = new System.Drawing.Point(7, 46);
    label7.Size = new System.Drawing.Size(95, 13);
    label7.TabIndex = 0;
    label7.Text = "Lebar Foto (Piksel)";
    // 
    // label8
    // 
    label8.Location = new System.Drawing.Point(7, 70);
    label8.Size = new System.Drawing.Size(97, 13);
    label8.TabIndex = 0;
    label8.Text = "Tinggi Foto (Piksel)";
    // 
    // txtTujuanTinggi
    // 
    txtTujuanTinggi.Location = new System.Drawing.Point(121, 70);
    txtTujuanTinggi.Name = "txtTujuanTinggi";
    txtTujuanTinggi.Size = new System.Drawing.Size(134, 20);
    txtTujuanTinggi.TabIndex = 1;
    txtTujuanTinggi.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
    // 
    // groupBox3
    // 
    groupBox3.Controls.Add(cbInterpolasi);
    groupBox3.Controls.Add(cbPenghalusan);
    groupBox3.Controls.Add(label9);
    groupBox3.Controls.Add(label10);
    groupBox3.Location = new System.Drawing.Point(16, 163);
    groupBox3.Size = new System.Drawing.Size(261, 96);
    groupBox3.TabIndex = 3;
    groupBox3.TabStop = false;
    groupBox3.Text = "Opsi";
    // 
    // label9
    // 
    label9.AutoSize = true;
    label9.Location = new System.Drawing.Point(7, 31);
    label9.Name = "label9";
    label9.Size = new System.Drawing.Size(99, 13);
    label9.TabIndex = 0;
    label9.Text = "Mode Penghalusan";
    // 
    // cbPenghalusan
    // 
    cbPenghalusan.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    cbPenghalusan.FormattingEnabled = true;
    cbPenghalusan.Location = new System.Drawing.Point(121, 31);
    cbPenghalusan.Name = "cbPenghalusan";
    cbPenghalusan.Size = new System.Drawing.Size(134, 21);
    cbPenghalusan.TabIndex = 1;
    cbPenghalusan.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    // 
    // label10
    // 
    label10.Location = new System.Drawing.Point(7, 54);
    label10.Size = new System.Drawing.Size(85, 13);
    label10.TabIndex = 0;
    label10.Text = "Mode Interpolasi";
    // 
    // cbInterpolasi
    // 
    cbInterpolasi.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    cbInterpolasi.FormattingEnabled = true;
    cbInterpolasi.Location = new System.Drawing.Point(121, 54);
    cbInterpolasi.Name = "cbInterpolasi";
    cbInterpolasi.Size = new System.Drawing.Size(134, 21);
    cbInterpolasi.TabIndex = 1;
    cbInterpolasi.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    // 
    // groupBox4
    // 
    groupBox4.Controls.Add(btnSelesai);
    groupBox4.Controls.Add(btnProses);
    groupBox4.Controls.Add(progressBar1);
    groupBox4.Location = new System.Drawing.Point(283, 163);
    groupBox4.Size = new System.Drawing.Size(261, 96);
    groupBox4.TabIndex = 3;
    groupBox4.TabStop = false;
    groupBox4.Text = "Proses";
    // 
    // progressBar1
    // 
    progressBar1.Location = new System.Drawing.Point(10, 28);
    progressBar1.Size = new System.Drawing.Size(248, 23);
    progressBar1.TabIndex = 0;
    // 
    // btnProses
    // 
    btnProses.Location = new System.Drawing.Point(53, 57);
    btnProses.Size = new System.Drawing.Size(75, 23);
    btnProses.TabIndex = 1;
    btnProses.Text = "Proses";
    // 
    // btnSelesai
    // 
    btnSelesai.Location = new System.Drawing.Point(134, 57);
    btnSelesai.Size = new System.Drawing.Size(75, 23);
    btnSelesai.TabIndex = 1;
    btnSelesai.Text = "Selesai";
    // 
    // PhotoResizer Form
    // 
    Controls.Add(groupBox2);
    Controls.Add(groupBox4);
    Controls.Add(groupBox3);
    Controls.Add(groupBox1);
    Controls.Add(btnAmbil);
    Controls.Add(txtFotoTujuan);
    Controls.Add(txtFotoAsal);
    Controls.Add(label2);
    Controls.Add(label1);
    Size = new System.Drawing.Size(563, 300);
    Text = "PhotoResizer: Ayo Belajar C# (abcsharpind.blogspot.com)";
    
    //Inisialisasi
    cbPenghalusan.Items.Clear();
    cbPenghalusan.Items.Add("Default");
    cbPenghalusan.Items.Add("HighSpeed");
    cbPenghalusan.Items.Add("HighQuality");
    cbPenghalusan.SelectedIndex=0;

    cbInterpolasi.Items.Clear();
    cbInterpolasi.Items.Add("Default");
    cbInterpolasi.Items.Add("Bilinear");
    cbInterpolasi.Items.Add("Bicubic");
    cbInterpolasi.Items.Add("High");
    cbInterpolasi.Items.Add("HighQualityBilinear");
    cbInterpolasi.Items.Add("HighQualityBicubic");
    cbInterpolasi.Items.Add("Low");
    cbInterpolasi.Items.Add("NearestNeighbour");
    cbInterpolasi.SelectedIndex = 0;
    
    txtTujuanLebar.Text = "640";
    txtTujuanTinggi.Text = "480";
    //Penambahan Aksi Tombol:
    btnAmbil.Click += new System.EventHandler(this.btnAmbil_Click);    
    btnProses.Click += new System.EventHandler(this.btnProses_Click);
    btnSelesai.Click += new System.EventHandler(this.btnSelesai_Click);
    
  }
  private void btnAmbil_Click(object sender, EventArgs e)
  {
      OpenFileDialog ofd;
      ofd = new OpenFileDialog();
      ofd.Filter = "JPeg File (*.jpg)|*.jpg||";            
      if (ofd.ShowDialog() == DialogResult.OK )
      {
          String tx = ofd.FileName;
          txtFotoAsal.Text = ofd.FileName;
          tx = tx.Substring(0,ofd.FileName.Length-ofd.SafeFileName.Length);
          txtFotoTujuan.Text = tx + "Dest-" + ofd.SafeFileName;
          System.IO.FileInfo f = new System.IO.FileInfo(ofd.FileName);
          long size = f.Length;                
          Bitmap b = new Bitmap(ofd.FileName);
          txtAsalUkuran.Text = size.ToString("#,##0");
          txtAsalLebar.Text = b.Width.ToString("#,##0");
          txtAsalTinggi.Text = b.Height.ToString("#,##0");
          b.Dispose();                
      }
      
  }

  private void btnSelesai_Click(object sender, EventArgs e)
  {
      Close();
  }

  private void btnProses_Click(object sender, EventArgs e)
  {
      int w;
      int h;

      progressBar1.Value = 10;
      int.TryParse( txtTujuanLebar.Text, out w);
      int.TryParse(txtTujuanTinggi.Text, out h);


      progressBar1.Value = 20;
      Bitmap bmpAsal = new Bitmap(txtFotoAsal.Text);
      Bitmap bmpTujuan = new Bitmap(w, h, bmpAsal.PixelFormat);
      Graphics g = Graphics.FromImage(bmpTujuan);


      progressBar1.Value = 30;
      //Smoothing Mode
      switch (cbPenghalusan.SelectedIndex)
      {
          case 0:
              g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
              break;
          case 1:
              g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;
              break;
          case 2:
              g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
              break;
      }

      progressBar1.Value = 40;

      switch (cbInterpolasi.SelectedIndex)
      {
          case 0:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
              break;
          case 1:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
              break;
          case 2:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bicubic;
              break;
          case 3:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
              break;
          case 4:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
              break;
          case 5:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
              break;
          case 6:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
              break;
          case 7:
              g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
              break;
      }

      progressBar1.Value = 50;
      
      g.DrawImage(bmpAsal, 0, 0, w, h);
      g.Dispose();

      progressBar1.Value = 60;
      System.Drawing.Imaging.ImageCodecInfo[] c = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders(); ;
      System.Drawing.Imaging.ImageCodecInfo ss = c[0];
      for (int i = 0; i < c.Length; i++)
      {
          if (c[i].MimeType.Equals("image/jpeg"))
          {
              ss =  c[i];
          }
      }
      progressBar1.Value = 70;
      bmpTujuan.Save(txtFotoTujuan.Text, ss, null);

      progressBar1.Value = 80;
      bmpAsal.Dispose();
      bmpTujuan.Dispose();

      progressBar1.Value = 90;

      System.IO.FileInfo fi = new System.IO.FileInfo(txtFotoTujuan.Text);
      txtTujuanUkuran.Text = fi.Length.ToString("#,##0");
      progressBar1.Value = 100;
      
  }  

  public static void Main()
  {
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  PhotoResizer p ;
  p = new PhotoResizer();
  Application.Run(p);    
  }

}
Ok, Selamat Mencoba!!! (PhotoResizer)

0 comments:

Post a Comment