
画像の拡大と縮小
前回作成した簡易画像編集ソフトはファイルを開くことと保存することしかできず、肝心の編集機能がありませんでした。今回は画像の拡大と縮小をおこないます。
SizeChangeメソッドの作成
SizeChangeメソッドはサイズを変更するためのダイアログを表示し、実際にサイズの変更をおこないます。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public partial class Form1 : Form {     private void SizeChangeMenuItem_Click(object sender, EventArgs e)     {         SizeChange();     }     void SizeChange()     {         Bitmap bmp = userControlImage1.Bitmap;         if(bmp == null)             return;         SizeChangeForm form = new SizeChangeForm();         form.bmpWidth = bmp.Width;         form.bmpHeight = bmp.Height;         form.Text = "サイズ変更";         if(form.ShowDialog() == DialogResult.OK)         {             userControlImage1.SizeChange(form.newWidth, form.newHeight);         }         form.Dispose();     } } | 
実際に画像のサイズ変更をするメソッドは以下のとおりです。新しい幅と高さを指定してサイズを変更します。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public partial class UserControlImage : UserControl {     public void SizeChange(int newWidth, int newHeight)     {         Bitmap newBmp = new Bitmap(newWidth, newHeight);         Graphics g = Graphics.FromImage(newBmp);         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;         g.DrawImage(Bitmap, 0, 0, newWidth, newHeight);         g.Dispose();         Bitmap = newBmp;         ShowBitmap(Bitmap);     } } | 
SizeChangeFormクラスの作成
SizeChangeFormクラスはサイズ変更のためのフォームを管理するためのものです。縦横のピクセル数で指定することも、拡大縮小率で指定することもできます。また縦横の比率を変えずにサイズ変更をすることもできます。

コンストラクタのなかで、[OK]ボタンと[キャンセル]ボタンがおされるとDialogResult.OKとDialogResult.Cancelを返すようにしています。
また[ピクセルで指定][拡大率で指定]が選択されている場合、他方のコントロールは操作できないようにしています。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public partial class SizeChangeForm : Form {     // 編集中のビットマップの幅と高さ     public int bmpWidth = 0;     public int bmpHeight = 0;     // 設定されたビットマップの幅と高さ     public int newWidth = 0;     public int newHeight = 0;     public SizeChangeForm()     {         InitializeComponent();         buttonOK.DialogResult = DialogResult.OK;         buttonCancel.DialogResult = DialogResult.Cancel;         buttonOK.Click += ButtonOK_Click;         pixelRadioButton.CheckedChanged += PixelRadioButton_CheckedChanged;         percentRadioButton.CheckedChanged += PercentRadioButton_CheckedChanged;         pixelWidthUpDown.ValueChanged += PixelWidthUpDown_ValueChanged;         pixelHeightUpDown.ValueChanged += PixelHeightUpDown_ValueChanged;         percentWidthUpDown.ValueChanged += PercentWidthUpDown_ValueChanged;         percentHeightUpDown.ValueChanged += PercentHeightUpDown_ValueChanged;         checkBox1.CheckedChanged += CheckBox1_CheckedChanged;         pixelRadioButton.Checked = true;     } } | 
サイズ変更を設定するためのダイアログには現在の画像のサイズを表示させる必要があります。イベントハンドラSizeChangeForm_Loadは、大きさが変更される前の画像のサイズをダイアログに表示させます。
| 1 2 3 4 5 6 7 8 9 10 11 | public partial class SizeChangeForm : Form {     private void SizeChangeForm_Load(object sender, EventArgs e)     {         percentWidthUpDown.Value = 100;         percentHeightUpDown.Value = 100;         pixelWidthUpDown.Value = bmpWidth;         pixelHeightUpDown.Value = bmpHeight;     } } | 
イベントハンドラCheckBox1_CheckedChangedは[縦横の比率をそろえる]のチェックボックスのチェック状態が変更されると呼び出されます。
[縦横の比率をそろえる]が選択されている場合は縦横の拡大縮小率を同じにし、それ以外のコントロールに入力されている値も適切な値に変更しています。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public partial class SizeChangeForm : Form {     private void CheckBox1_CheckedChanged(object sender, EventArgs e)     {         if(checkBox1.Checked)         {             percentHeightUpDown.Enabled = false;             pixelHeightUpDown.Enabled = false;             decimal percent = percentWidthUpDown.Value;             percentHeightUpDown.Value = percent;             pixelHeightUpDown.Value = bmpHeight * percent / 100;         }         else         {             if(pixelRadioButton.Checked)                 pixelHeightUpDown.Enabled = true;             else                 percentHeightUpDown.Enabled = true;         }     } } | 
ピクセルで指定する場合、値が変更されると拡大縮小率も適切な値に変更する必要があります。逆の場合も同様です。以下のイベントハンドラはそのための処理をするためのものです。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | public partial class SizeChangeForm : Form {     private void PixelWidthUpDown_ValueChanged(object sender, EventArgs e)     {         if(pixelWidthUpDown.Enabled)             percentWidthUpDown.Value = 100 * pixelWidthUpDown.Value / bmpWidth;         if(checkBox1.Checked)         {             decimal percent = 100 * pixelWidthUpDown.Value / bmpWidth;             percentHeightUpDown.Value = percent;             pixelHeightUpDown.Value = bmpHeight * percent / 100;         }     }     private void PixelHeightUpDown_ValueChanged(object sender, EventArgs e)     {         if(pixelHeightUpDown.Enabled)             percentHeightUpDown.Value = 100 * pixelHeightUpDown.Value / bmpHeight;     }     private void PercentWidthUpDown_ValueChanged(object sender, EventArgs e)     {         if(percentWidthUpDown.Enabled)             pixelWidthUpDown.Value = bmpWidth * percentWidthUpDown.Value / 100;         if(checkBox1.Checked)         {             decimal percent = percentWidthUpDown.Value;             percentHeightUpDown.Value = percent;             pixelHeightUpDown.Value = bmpHeight * percent / 100;         }     }     private void PercentHeightUpDown_ValueChanged(object sender, EventArgs e)     {         if(percentHeightUpDown.Enabled)             pixelHeightUpDown.Value = bmpHeight * percentHeightUpDown.Value / 100;     } } | 
PixelRadioButton_CheckedChangedイベントハンドラは、ラジオボタンの[ピクセルで指定]、[拡大縮小率で指定]の選択が変更されたときに呼び出されます。PixelRadioButton_CheckedChangedはNumericUpDownコントロールを有効、無効を切り替えるためのものです。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public partial class SizeChangeForm : Form {     private void PixelRadioButton_CheckedChanged(object sender, EventArgs e)     {         if(pixelRadioButton.Checked)         {             pixelWidthUpDown.Enabled = true;             pixelHeightUpDown.Enabled = true;             percentWidthUpDown.Enabled = false;             percentHeightUpDown.Enabled = false;         }         else         {             pixelWidthUpDown.Enabled = false;             pixelHeightUpDown.Enabled = false;             percentWidthUpDown.Enabled = true;             percentHeightUpDown.Enabled = true;         }     } } | 
イベントハンドラButtonOK_Clickは[OK]がクリックされたときに設定された値を保存します。これによって編集中の画像に変更を反映させることができるようになります。
| 1 2 3 4 5 6 7 8 | public partial class SizeChangeForm : Form {     private void ButtonOK_Click(object sender, EventArgs e)     {         newWidth = (int)pixelWidthUpDown.Value;         newHeight = (int)pixelHeightUpDown.Value;     } } | 
