
前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
//★ 顔画像のサイズを整える 前田 稔
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Imaging;
public class MyForm : Form
{
Bitmap bmp;
Bitmap mark;
string ImgFile = "";
int x0=0, y0=0, x1=0, y1=0;
public MyForm()
{
Text = "画像のサイズを整える";
BackColor = SystemColors.AppWorkspace;
OpenFileDialog opendlg = new OpenFileDialog();
opendlg.Filter = "画像ファイル (*.bmp)|*.bmp|すべてのファイル (*.*)|*.*" ;
if (opendlg.ShowDialog() == DialogResult.OK)
{ ImgFile = opendlg.FileName; }
try
{ bmp = new Bitmap(ImgFile); }
catch
{ MessageBox.Show("画像ファイルが読めません!", ImgFile);
return ;
}
mark = new Bitmap("C:\\DATA\\Test\\mark.gif");
Width = bmp.Width;
Height = bmp.Height+32;
Paint += new PaintEventHandler(MyHandler);
MouseDown += new MouseEventHandler(OnMyMouseDown);
FormClosing += new FormClosingEventHandler(Form_Closing);
}
private void MyHandler(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(bmp, new Point(0,0));
if (x0!=0) g.DrawImage(mark, new Point(x0,y0));
if (x1!=0) g.DrawImage(mark, new Point(x1,y1));
}
private void OnMyMouseDown(object sender, MouseEventArgs e)
{ if (x1!=0)
{ x0=y0=x1=y1= 0;
Invalidate();
return;
}
if (x0==0)
{ x0= e.X;
y0= e.Y;
Invalidate();
return;
}
x1= e.X;
y1= e.Y;
Invalidate();
}
private void Form_Closing(object sender, FormClosingEventArgs e)
{ Bitmap b256 = new Bitmap(256, 256);
int wk;
if (x1==0) return;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "保存するファイルを選択してください";
saveFileDialog1.Filter = "BMP FILE|*.bmp|すべてのファイル|*.*";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{ wk = y0 - y1;
Rectangle des = new Rectangle(0, 0, 256, 256);
Rectangle sou = new Rectangle(0, 0, wk*4, wk*4);
sou.X= x0-wk-wk;
sou.Y= y0-wk-wk;
Graphics g= Graphics.FromImage(b256);
g.DrawImage(bmp, des, sou, GraphicsUnit.Pixel);
b256.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
}
}
class image01
{
[STAThread]
public static void Main()
{
MyForm mf = new MyForm();
Application.Run(mf);
}
}
|
using System.Drawing.Imaging;
・・・
{ Bitmap b256 = new Bitmap(256, 256);
・・・
b256.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
|
[STAThread]
public static void Main()
・・・
OpenFileDialog opendlg = new OpenFileDialog();
opendlg.Filter = "画像ファイル (*.bmp)|*.bmp|すべてのファイル (*.*)|*.*" ;
if (opendlg.ShowDialog() == DialogResult.OK)
{ ImgFile = opendlg.FileName; }
|
public class MyForm : Form
{
Bitmap bmp;
Bitmap mark;
string ImgFile = "";
int x0=0, y0=0, x1=0, y1=0;
|
public MyForm()
{
・・・
OpenFileDialog opendlg = new OpenFileDialog();
opendlg.Filter = "画像ファイル (*.bmp)|*.bmp|すべてのファイル (*.*)|*.*" ;
if (opendlg.ShowDialog() == DialogResult.OK)
{ ImgFile = opendlg.FileName; }
・・・
mark = new Bitmap("C:\\DATA\\Test\\mark.gif");
Width = bmp.Width;
Height = bmp.Height+32;
Paint += new PaintEventHandler(MyHandler);
MouseDown += new MouseEventHandler(OnMyMouseDown);
FormClosing += new FormClosingEventHandler(Form_Closing);
|
private void MyHandler(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(bmp, new Point(0,0));
if (x0!=0) g.DrawImage(mark, new Point(x0,y0));
if (x1!=0) g.DrawImage(mark, new Point(x1,y1));
}
|
private void OnMyMouseDown(object sender, MouseEventArgs e)
{ if (x1!=0)
{ x0=y0=x1=y1= 0;
Invalidate();
return;
}
if (x0==0)
{ x0= e.X;
y0= e.Y;
Invalidate();
return;
}
x1= e.X;
y1= e.Y;
Invalidate();
}
|
private void Form_Closing(object sender, FormClosingEventArgs e)
{ Bitmap b256 = new Bitmap(256, 256);
int wk;
if (x1==0) return;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "保存するファイルを選択してください";
saveFileDialog1.Filter = "BMP FILE|*.bmp|すべてのファイル|*.*";
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{ wk = y0 - y1;
Rectangle des = new Rectangle(0, 0, 256, 256);
Rectangle sou = new Rectangle(0, 0, wk*4, wk*4);
sou.X= x0-wk-wk;
sou.Y= y0-wk-wk;
Graphics g= Graphics.FromImage(b256);
g.DrawImage(bmp, des, sou, GraphicsUnit.Pixel);
b256.Save(saveFileDialog1.FileName, ImageFormat.Bmp);
}
}
|
g.DrawImage(bmp, des, sou, GraphicsUnit.Pixel); bmp: Bitmap Image des: 受け取り側の矩形(256*256) sou: 送り側の矩形(鼻の頭を中心に wk*4 の幅と高さ) Pixel: イメージデータ(ピクセル)の並び |
![]()
[Next Chapter ↓] Color ⇒ Grayscale
[Previous Chapter ↑] Image Guid