まえがき
Visual StudioでC#アプリケーションを作成するための備忘録
仕様
①
・ボタンのイベントで画像を選択し、PictureBoxに選択画像を表示する。
・選択画像とPictureBoxのサイズを自動判別し、PictureBox内に選択画像を全面表示する
・出力画像名を入力ファイル名から再生成する。
②
・ボタンのイベントで画像処理用のメモリを確保
・特定の画像処理を行う
・処理後の画像をPictureBoxに表示する
③
・ボタンのイベントで処理後の画像を保存する
ソース
①
private void button1_Click(object sender, EventArgs e)
{
///////////////////////////////////////////////////////
/// 画像ファイルを選択
///////////////////////////////////////////////////////
//ファイルを開くダイアログボックスの作成
var ofd = new OpenFileDialog();
//ファイルフィルタ
ofd.Filter = "Image File(*.bmp,*.jpg,*.png,*.tif)|*.bmp;*.jpg;*.png;*.tif|Bitmap(*.bmp)|*.bmp|Jpeg(*.jpg)|*.jpg|PNG(*.png)|*.png";
//ダイアログの表示 (Cancelボタンがクリックされた場合は何もしない)
if (ofd.ShowDialog() == DialogResult.Cancel)
{
}
//画像ファイル名の取得
str_img_full_path = ofd.FileName;
//ディレクトリ名の取得
str_img_dir = Path.GetDirectoryName(str_img_full_path);
//ファイル名の取得(拡張子なし)
str_img_file = Path.GetFileNameWithoutExtension(str_img_full_path);
//ファイル名の取得(拡張子あり)
label1.Text = Path.GetFileName(str_img_full_path);
//出力ファイル名の生成
str_out_file = str_img_file + str_out_suffix;
label2.Text = str_out_file;
//フルパスの生成
str_out_full_path = str_img_dir + "\\" + str_out_file;
///////////////////////////////////////////////////////
/// 画像をPictureBoxに表示する
/// ///////////////////////////////////////////////////////
pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
//画像ファイルの読み込み
isrc = new Bitmap(ofd.FileName);
//縮小サイズの計算(最大サイズに合わせて縮小)
double scale_x = ((double)isrc.Width / (double)pictureBox1.Width);
double scale_y = ((double)isrc.Height / (double)pictureBox1.Height);
double scale = (scale_x > scale_y) ? scale_x : scale_y;
//リサイズ画像の作成
Bitmap bmpResize = new Bitmap(isrc, (int)(isrc.Width / scale), (int)(isrc.Height / scale));
var g = Graphics.FromImage(pictureBox1.Image);
g.DrawImage(bmpResize, 0, 0, bmpResize.Width, bmpResize.Height);
}
②
private void button3_Click(object sender, EventArgs e)
{
///////////////////////////////////////////////////////
/// 画像処理用メモリを確保する
///////////////////////////////////////////////////////
// Bitmapをロック
var bmpData = isrc.LockBits(new Rectangle(0, 0, isrc.Width, isrc.Height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
isrc.PixelFormat
);
// メモリの幅のバイト数を取得
var stride = Math.Abs(bmpData.Stride);
// チャンネル数取得
var channel = Bitmap.GetPixelFormatSize(isrc.PixelFormat) / 8;
// 画像格納用配列
var src_data = new byte[stride * bmpData.Height];
var dst_data = new byte[stride * bmpData.Height];
// Bitmapデータをsrc配列へコピー
System.Runtime.InteropServices.Marshal.Copy(
bmpData.Scan0,
src_data,
0,
src_data.Length
);
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//ここにHLS用関数を追加する
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//Pixel->Beat変換
int index;
// 移動平均処理
for (int j = 1; j < bmpData.Height - 1; j++)
{
for (int i = channel; i < (bmpData.Width - 1) * channel; i++)
{
index = i + j * stride;
dst_data[index]
= (byte)((
src_data[index - channel - stride] + src_data[index - stride] + src_data[index + channel - stride]
+ src_data[index - channel] + src_data[index] + src_data[index + channel]
+ src_data[index - channel + stride] + src_data[index + stride] + src_data[index + channel + stride]
) / 9);
}
}
// 配列をBitmapデータへコピー
System.Runtime.InteropServices.Marshal.Copy(
dst_data,
0,
bmpData.Scan0,
dst_data.Length
);
// アンロック
isrc.UnlockBits(bmpData);
///////////////////////////////////////////////////////
/// 処理画像をPictureBox2に表示する
/// ///////////////////////////////////////////////////////
pictureBox2.Image = new Bitmap(pictureBox2.Width, pictureBox2.Height);
//縮小サイズの計算(最大サイズに合わせて縮小)
double scale_x = ((double)isrc.Width / (double)pictureBox1.Width);
double scale_y = ((double)isrc.Height / (double)pictureBox1.Height);
double scale = (scale_x > scale_y) ? scale_x : scale_y;
//リサイズ画像の作成
Bitmap bmpResize = new Bitmap(isrc, (int)(isrc.Width / scale), (int)(isrc.Height / scale));
var g = Graphics.FromImage(pictureBox2.Image);
g.DrawImage(bmpResize, 0, 0, bmpResize.Width, bmpResize.Height);
}
③
private void button4_Click(object sender, EventArgs e)
{
isrc.Save(str_out_full_path,System.Drawing.Imaging.ImageFormat.Bmp);
label2.Text = "Complete Save Image";
}