📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace GrayScale
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pb_color;
private System.Windows.Forms.Button btn_convert;
private System.Windows.Forms.Button btn_load;
private System.Windows.Forms.Button btn_save;
private System.Windows.Forms.PictureBox pb_grayscale;
/// <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.pb_color = new System.Windows.Forms.PictureBox();
this.pb_grayscale = new System.Windows.Forms.PictureBox();
this.btn_convert = new System.Windows.Forms.Button();
this.btn_load = new System.Windows.Forms.Button();
this.btn_save = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// pb_color
//
this.pb_color.Location = new System.Drawing.Point(0, 32);
this.pb_color.Name = "pb_color";
this.pb_color.Size = new System.Drawing.Size(232, 232);
this.pb_color.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pb_color.TabIndex = 0;
this.pb_color.TabStop = false;
//
// pb_grayscale
//
this.pb_grayscale.Location = new System.Drawing.Point(312, 32);
this.pb_grayscale.Name = "pb_grayscale";
this.pb_grayscale.Size = new System.Drawing.Size(248, 232);
this.pb_grayscale.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pb_grayscale.TabIndex = 1;
this.pb_grayscale.TabStop = false;
//
// btn_convert
//
this.btn_convert.ForeColor = System.Drawing.Color.Black;
this.btn_convert.Location = new System.Drawing.Point(232, 88);
this.btn_convert.Name = "btn_convert";
this.btn_convert.Size = new System.Drawing.Size(75, 56);
this.btn_convert.TabIndex = 2;
this.btn_convert.Text = "Convert";
this.btn_convert.Click += new System.EventHandler(this.btn_convert_Click);
//
// btn_load
//
this.btn_load.Location = new System.Drawing.Point(0, 0);
this.btn_load.Name = "btn_load";
this.btn_load.Size = new System.Drawing.Size(232, 23);
this.btn_load.TabIndex = 3;
this.btn_load.Text = "Load Image";
this.btn_load.Click += new System.EventHandler(this.btn_load_Click);
//
// btn_save
//
this.btn_save.Location = new System.Drawing.Point(312, 0);
this.btn_save.Name = "btn_save";
this.btn_save.Size = new System.Drawing.Size(248, 23);
this.btn_save.TabIndex = 4;
this.btn_save.Text = "Save Image";
this.btn_save.Click += new System.EventHandler(this.btn_save_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(560, 266);
this.Controls.Add(this.btn_save);
this.Controls.Add(this.btn_load);
this.Controls.Add(this.btn_convert);
this.Controls.Add(this.pb_grayscale);
this.Controls.Add(this.pb_color);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btn_load_Click(object sender, System.EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (open.ShowDialog()==DialogResult.OK)
{
pb_color.Image = new Bitmap(open.FileName);
btn_convert.Enabled=true;
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
btn_convert.Enabled=false;
btn_save.Enabled=false;
}
private void btn_convert_Click(object sender, System.EventArgs e)
{
Bitmap grays = (Bitmap)pb_color.Image;
int width = grays.Size.Width;
int height = grays.Size.Height;
for (int j=0; j<height; j++)
{
for (int i=0; i<width; i++)
{
Color col;
col = grays.GetPixel(i, j);
grays.SetPixel(i,j,Color.FromArgb((col.R+col.G+col.B)/3,(col.R+col.G+col.B)/3,(col.R+col.G+col.B)/3 ));
}
}
pb_grayscale.Image = grays;
btn_save.Enabled=true;
}
private void btn_save_Click(object sender, System.EventArgs e)
{
Bitmap b = (Bitmap)pb_grayscale.Image;
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
if (save.ShowDialog()==DialogResult.OK)
{
b.Save(save.FileName);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -