//★ Grayscale で描画 前田 稔
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
public class MyForm : Form
{
Face App;
public MyForm()
{
BackColor = SystemColors.AppWorkspace;
App = new Face();
float hdpi = App.bmp.HorizontalResolution;
float vdpi = App.bmp.VerticalResolution;
Width = App.bmp.Width*96/(int)hdpi;
Height = (App.bmp.Height+32)*96/(int)vdpi;
Paint += new PaintEventHandler(MyHandler);
MouseDown += new MouseEventHandler(OnMyMouseDown);
}
private void MyHandler(object sender, PaintEventArgs e)
{
Graphics g;
g = e.Graphics;
if (App.bmp == null) Application.Exit();
App.View(g,0,0);
}
private void OnMyMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) //マウスの左ボタン
{ App.Gray(); }
if (e.Button == MouseButtons.Right) //マウスの右ボタン
{ App.LoadImage(); }
Invalidate();
}
}
//☆ Face Object Class
class Face
{
public Bitmap bmp = null;
string ImgFile = "";
// Constructor
public Face()
{
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 ;
}
}
// 画像ファイルの入力
public void LoadImage(string file)
{
bmp = new Bitmap(file);
}
public void LoadImage()
{
LoadImage(ImgFile);
}
// 画像を描画
public void View(Graphics g, int x, int y)
{
if (bmp != null) g.DrawImage(bmp, x, y);
}
// Grayscale で描画
public void Gray()
{ Color cor;
Int32 wk;
byte R,G,B;
if (bmp == null) return;
for(int y=0; y<bmp.Height; y++)
for(int x=0; x<bmp.Width; x++)
{ cor = bmp.GetPixel(x,y);
R = cor.R;
G = cor.G;
B = cor.B;
wk = (B*2 + G*4 + R) / 7;
cor = Color.FromArgb(wk, wk, wk);
bmp.SetPixel(x,y,cor);
}
}
}
class image01
{
[STAThread]
public static void Main()
{
MyForm mf = new MyForm();
Application.Run(mf);
}
}
|