
前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
//★ Win32 API Land Mark 前田 稔
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
struct BITMAPINFOHEADER
{
public UInt32 biSize;
public Int32 biWidth;
public Int32 biHeight;
public UInt16 biPlanes;
public UInt16 biBitCount;
public UInt32 biCompression;
public UInt32 biSizeImage;
public Int32 biXPelsPerMeter;
public Int32 biYPelsPerMeter;
public UInt32 biClrUsed;
public UInt32 biClrImportant;
};
unsafe struct BITMAPINFO
{
public BITMAPINFOHEADER bmih;
public fixed UInt32 bmiColors[1];
};
public class MyForm : Form
{ Face App;
public MyForm()
{
App = new Face();
App.OpenFile();
Width = App.m_bmp.Width;
Height = App.m_bmp.Height+32;
App.InitDib();
App.CopyBmp(App.m_DibDC);
App.CopyBmp(App.m_MaskDC);
Paint += new PaintEventHandler(MyHandler);
MouseDown += new MouseEventHandler(OnMyMouseDown);
}
private void MyHandler(object sender, PaintEventArgs e)
{
Graphics g;
g = e.Graphics;
if (App.m_bmp == null) Application.Exit();
App.ViewMask(g,0,0);
App.DrawRect(g);
App.DrawMark(g);
}
private void OnMyMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) //マウスの左ボタン
{ App.Edge(33000);
}
if (e.Button == MouseButtons.Right) //マウスの右ボタン
{ App.LandMark();
App.CopyBmp(App.m_MaskDC);
}
Invalidate();
}
public void VerMsg(string msg, int v)
{
string wstr;
wstr = msg + v.ToString();
this.Text = wstr;
}
}
//☆ Face Object Class
unsafe class Face
{
・・・
Face Object Class のソースは Mark Rect を参照して下さい。
・・・
|
public class MyForm : Form
{ Face App;
public MyForm()
{
App = new Face();
App.OpenFile();
Width = App.m_bmp.Width;
Height = App.m_bmp.Height+32;
App.InitDib();
App.CopyBmp(App.m_DibDC);
App.CopyBmp(App.m_MaskDC);
Paint += new PaintEventHandler(MyHandler);
MouseDown += new MouseEventHandler(OnMyMouseDown);
}
|
private void MyHandler(object sender, PaintEventArgs e)
{
Graphics g;
g = e.Graphics;
if (App.m_bmp == null) Application.Exit();
App.ViewMask(g,0,0);
App.DrawRect(g);
App.DrawMark(g);
}
|
private void OnMyMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) //マウスの左ボタン
{ App.Edge(33000);
}
if (e.Button == MouseButtons.Right) //マウスの右ボタン
{ App.LandMark();
App.CopyBmp(App.m_MaskDC);
}
Invalidate();
}
|
public Point[] m_pt = new Point[7]; //Land Mark Table
// Mark 検索パラメータを使って pt_t[7] にマーク座標を格納する
public void LandMark()
{
int[] t = new int[5];
m_pt[0].X = 128;
m_pt[0].Y = 128;
m_pt[1].X = 128;
m_pt[1].Y = 192;
for(int n=0; n<5; n++)
{ for(int i=0; i<5; i++) t[i] = mkt[n, i];
LandMK(t);
m_pt[n+2]= pt;
}
}
|
| パラメータ | 説明 |
|---|---|
| t[0], t[1] | 矩形領域の左下座標 |
| t[2], t[3] | 矩形領域の右上座標 |
| t[4] | 検索の方向(0:上, 1:下, 2:右, 3:左) |
// m_Mask の画像から t[5] の範囲で Land Mark を検出する
// t[0],t[1]=左下座標(X,Y), t[2],t[3]=右上座標(X,Y), t[4]=検索方向
public void LandMK(int[] t)
{ int w,h,cx,cy,x,y,x1,x2,y1,y2;
pt.X = pt.Y = 0;
w= t[2]-t[0];
h= t[3]-t[1];
cx= t[0]+w/2;
cy= t[1]+h/2;
switch(t[4])
{ case 0: //上方向
for(y=0; y<h; y++)
for(x=0; x<w/2; x++)
{
y1= t[1]+y;
x1= cx+x;
x2= cx-x;
if (MASK(x1,y1,0)==0)
{ pt.X= x1;
pt.Y= y1;
return;
}
if (MASK(x2,y1,0)==0)
{ pt.X= x2;
pt.Y= y1;
return;
}
}
break;
case 1: //下方向
for(y=0; y<h; y++)
for(x=0; x<w/2; x++)
{
y1= t[3]-y;
x1= cx+x;
x2= cx-x;
if (MASK(x1,y1,0)==0)
{ pt.X= x1;
pt.Y= y1;
return;
}
if (MASK(x2,y1,0)==0)
{ pt.X= x2;
pt.Y= y1;
return;
}
}
break;
case 2: //右方向
for(x=0; x<w; x++)
for(y=0; y<h/2; y++)
{
x1= t[0]+x;
y1= cy+y;
y2= cy-y;
if (MASK(x1,y1,0)==0)
{ pt.X= x1;
pt.Y= y1;
return;
}
if (MASK(x1,y2,0)==0)
{ pt.X= x1;
pt.Y= y2;
return;
}
}
break;
case 3: //左方向
for(x=0; x<w; x++)
for(y=0; y<h/2; y++)
{
x1= t[2]-x;
y1= cy+y;
y2= cy-y;
if (MASK(x1,y1,0)==0)
{ pt.X= x1;
pt.Y= y1;
return;
}
if (MASK(x1,y2,0)==0)
{ pt.X= x1;
pt.Y= y2;
return;
}
}
break;
}
}
}
|
![]()