
前田稔(Maeda Minoru)の超初心者のプログラム入門


| 
//★ Binary Mode で描画    前田 稔
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
public class MyForm : Form
{
    Face    App;
    int     val= 30;
    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.LoadImage();
            val+= 2;
            App.BinMode(val);
        }
        if (e.Button == MouseButtons.Right) //マウスの右ボタン
        {   App.LoadImage();
            val= 30;
        }
        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);
    }
    // Binary Mode で描画
    public void BinMode(int val)
    {   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;
                if (wk < val) cor = Color.Black;
                else cor = Color.White;
                bmp.SetPixel(x,y,cor);
            }
    }
}
class image01
{
    [STAThread]
    public static void Main()
    {
        MyForm mf = new MyForm();
        Application.Run(mf);
    }
}
 | 
| 青色(B)の8ビット | 緑色(G)の8ビット | 赤色(R)の8ビット | 
| 
    // Binary Mode で描画
    public void BinMode(int val)
    {   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;
                if (wk < val) cor = Color.Black;
                else cor = Color.White;
                bmp.SetPixel(x,y,cor);
            }
    }
 | 
| 
    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);
    }
 | 
| 
    int     val= 30;    // 閾値
    private void OnMyMouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)  //マウスの左ボタン
        {   App.LoadImage();
            val+= 2;
            App.BinMode(val);
        }
        if (e.Button == MouseButtons.Right) //マウスの右ボタン
        {   App.LoadImage();
            val= 30;
        }
        Invalidate();
    }
 | 

[Next Chapter ↓] Delete Color
[Previous Chapter ↑] Color ⇒ Grayscale