using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
namespace ConvertImage
{
///
/// Form1 の概要の説明です。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ProgressBar progressBar1;
//イメージエンコーダに関する情報
private System.Drawing.Imaging.ImageCodecInfo myImageCodecInfo;
///
/// 必要なデザイナ変数です。
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
///
/// 使用されているリソースに後処理を実行します。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
///
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
///
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.label1 = new System.Windows.Forms.Label();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"JPEG",
"PNG",
"GIF",
"TIFF",
"BMP"});
this.comboBox1.Location = new System.Drawing.Point(0, 0);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(88, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "JPEG";
this.comboBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboBox1_KeyPress);
//
// numericUpDown1
//
this.numericUpDown1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.numericUpDown1.Location = new System.Drawing.Point(90, 0);
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(40, 19);
this.numericUpDown1.TabIndex = 1;
this.numericUpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.numericUpDown1.Value = new System.Decimal(new int[] {
90,
0,
0,
0});
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 81);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1});
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(132, 22);
this.statusBar1.SizingGrip = false;
this.statusBar1.TabIndex = 2;
this.statusBar1.DrawItem += new System.Windows.Forms.StatusBarDrawItemEventHandler(this.statusBar1_DrawItem);
//
// statusBarPanel1
//
this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.statusBarPanel1.Style = System.Windows.Forms.StatusBarPanelStyle.OwnerDraw;
this.statusBarPanel1.Width = 132;
//
// label1
//
this.label1.Anchor = System.Windows.Forms.AnchorStyles.None;
this.label1.Font = new System.Drawing.Font("MS UI Gothic", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(128)));
this.label1.Location = new System.Drawing.Point(20, 28);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(92, 46);
this.label1.TabIndex = 3;
this.label1.Text = "ここに画像をDrag&&Drop";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(16, 56);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Step = 1;
this.progressBar1.TabIndex = 4;
//
// Form1
//
this.AllowDrop = true;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(132, 103);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.label1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.comboBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "画像変換";
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// アプリケーションのメイン エントリ ポイントです。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
#region イベントハンドラ
private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = true;
}
private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
switch (this.comboBox1.Text)
{
case "JPEG":
myImageCodecInfo = GetEncoderInfo("image/jpeg");
break;
case "PNG":
myImageCodecInfo = GetEncoderInfo("image/png");
break;
case "GIF":
myImageCodecInfo = GetEncoderInfo("image/gif");
break;
case "TIFF":
myImageCodecInfo = GetEncoderInfo("image/tiff");
break;
case "BMP":
myImageCodecInfo = GetEncoderInfo("image/bmp");
break;
default:
MessageBox.Show("画像形式を正しく指定してください。");
return;
}
//コントロール内にドロップされたとき実行される
//ドロップされたすべてのファイル名を取得する
string[] fileNames = (string[]) e.Data.GetData(DataFormats.FileDrop, false);
this.progressBar1.Maximum = fileNames.Length;
foreach (string fileName in fileNames)
{
Debug.WriteLine(fileName);
this.progressBar1.PerformStep();
this.SaveImage(fileName, (int)this.numericUpDown1.Value);
}
this.progressBar1.Value = 0;
}
private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
//Cursor.Current = Cursors.AppStarting;
//コントロール内にドラッグされたとき実行される
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
//ドラッグされたデータ形式を調べ、ファイルのときはコピーとする
e.Effect = DragDropEffects.Copy;
}
else
{
//ファイル以外は受け付けない
e.Effect = DragDropEffects.None;
}
}
private void statusBar1_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
if (sbdevent.Panel == this.statusBarPanel1)
{
StatusBar sb = (StatusBar)sender;
Rectangle rect = sbdevent.Bounds;
this.progressBar1.SetBounds(sb.Location.X + rect.X, sb.Location.Y + rect.Y,
rect.Width, rect.Height);
}
}
#endregion
///
/// 指定されたファイルを品質を指定して新規ファイル形式で保存する
///
/// 画像ファイル名
/// 品質
private void SaveImage(string fileName, int quality)
{
Bitmap myBitmap = null;
try
{
//画像ファイルを読み込む
myBitmap = new Bitmap(fileName);
}
catch (Exception e)
{
MessageBox.Show(fileName + " は\n画像ファイルじゃないらしいよ。",
"File Read Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Trace.WriteLine(e);
return;
}
//EncoderParameterオブジェクトを1つ格納できる
//EncoderParametersクラスの新しいインスタンスを初期化
//ここでは品質のみ指定するため1つだけ用意する
System.Drawing.Imaging.EncoderParameters myEncoderParameters =
new System.Drawing.Imaging.EncoderParameters(1);
//品質を指定
System.Drawing.Imaging.EncoderParameter myEncoderParameter =
new System.Drawing.Imaging.EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, quality);
//EncoderParametersにセットする
myEncoderParameters.Param[0] = myEncoderParameter;
//新しいファイルの拡張子を取得する
string ext = myImageCodecInfo.FilenameExtension.Split(';')[0];
ext = System.IO.Path.GetExtension(ext).ToLower();
//保存するファイル名を決定(拡張子を変える)
string saveName = System.IO.Path.ChangeExtension(fileName, ext);
//保存する
myBitmap.Save(saveName, myImageCodecInfo, myEncoderParameters);
myBitmap.Dispose();
}
///
/// MimeTypeで指定されたImageCodecInfoを探して返す
///
/// MIMEタイプ
private static System.Drawing.Imaging.ImageCodecInfo GetEncoderInfo(string mineType)
{
//GDI+ に組み込まれたイメージ エンコーダに関する情報をすべて取得
System.Drawing.Imaging.ImageCodecInfo [] myEncoders=
System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
//指定されたMimeTypeを探して見つかれば返す
foreach (System.Drawing.Imaging.ImageCodecInfo myEncoder in myEncoders)
if (myEncoder.MimeType == mineType)
return myEncoder;
return null;
}
}
}