📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
namespace image
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Bitmap b;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.PictureBox Pic;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem Process;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.Process = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.Pic = new System.Windows.Forms.PictureBox();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.Process});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem4});
this.menuItem1.Text = "File";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Open";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "-";
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "Exit";
//
// Process
//
this.Process.Enabled = false;
this.Process.Index = 1;
this.Process.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem7,
this.menuItem8,
this.menuItem5,
this.menuItem6});
this.Process.Text = "Processing";
//
// menuItem7
//
this.menuItem7.Index = 0;
this.menuItem7.Text = "GrayScale";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// menuItem8
//
this.menuItem8.Index = 1;
this.menuItem8.Text = "-";
//
// Pic
//
this.Pic.Location = new System.Drawing.Point(8, 8);
this.Pic.Name = "Pic";
this.Pic.Size = new System.Drawing.Size(144, 144);
this.Pic.TabIndex = 0;
this.Pic.TabStop = false;
//
// menuItem5
//
this.menuItem5.Index = 2;
this.menuItem5.Text = "Binary";
this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);
//
// menuItem6
//
this.menuItem6.Index = 3;
this.menuItem6.Text = "-";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.Pic});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Connected Components";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
OpenFileDialog dlg=new OpenFileDialog();
dlg.Title="Sample";
string file="";
dlg.Filter="Bitmaps/Gif/jpeg (*.bmp;*.gif)|*.gif;*.bmp;*.jpg";
if (dlg.ShowDialog()== DialogResult.OK)
{
file=dlg.FileName;
}
else
{
MessageBox.Show("Please Enter a Valid File");
menuItem2_Click(sender,e);
}
b=new Bitmap(file);
Pic.Width=b.Width;
Pic.Height=b.Height;
Pic.Image=b;
this.Process.Enabled=true;
}
private void menuItem7_Click(object sender, System.EventArgs e)
{
GrayScale(b);
Pic.Image=b;
}
public bool GrayScale(Bitmap b)
{
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0,0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride; //the length of the line
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte * p = (byte*)(void*)Scan0;
int nOffset = stride - b.Width*3;
byte red, green, blue;
for(int y=0;y < b.Height;++y)
{
for(int x=0; x < b.Width; ++x )
{
blue = p[0];
green = p[1];
red = p[2];
p[0] = p[1] = p[2] = (byte)(.299 * red
+ .587 * green
+ .114 * blue);
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
Binary(b,true);
Pic.Image=b;
}
public bool Binary(Bitmap b,bool flag)
{
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride; //the length of the line
System.IntPtr Scan0 = bmData.Scan0;
int Threshold=220;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - b.Width*3;
byte red, green, blue;
byte binary;
for(int y=0;y < b.Height;++y)
{
for(int x=0; x < b.Width; ++x )
{
blue = p[0];
green = p[1];
red = p[2];
binary= (byte)(.299 * red
+ .587 * green
+ .114 * blue);
if (binary<Threshold&&flag)
p[0] = p[1] = p[2] = 0;
else
if (binary>=Threshold&&flag)
p[0] = p[1] = p[2] = 255;
else
if (binary<Threshold&&!flag)
p[0] = p[1] = p[2] = 255;
else
p[0] = p[1] = p[2] = 0;
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -