//Form.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
/*
彩色图像灰度化的转换公式有三种
1、Gray(i,j)=[R(i,j)+G(i,j)+B(i,j)]/3
2、Gray(i,j)=0.299*R(i,j)+0.587*G(i,j)+0.114*B(i,j)
3、Gray(i,j)=G(i,j)由2知G占的比重最大,所以可以这样
*/
namespace colorToGray
{
public partial class Form1 : Form
{
private string curFileName;
private System.Drawing.Bitmap curBitmap;
private HiPerfTimer myTimer;
public Form1()
{
myTimer = new HiPerfTimer();
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog opnDlg = new OpenFileDialog(); //创建openFileDialog
//为图像选择一个筛选器
opnDlg.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;" +
"*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf|" +
"位图(*.bmp; *.jpg; *.png; ...) | *.bmp; *.pcx; *.png; *.jpg; *.gif; *.tif; *.ico|" +
"矢量图( *.wmf; *.eps; *.emf;...) | *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";
opnDlg.Title = "打开图像文件"; //设置对话框标题
opnDlg.ShowHelp = true;
if (opnDlg.ShowDialog() == DialogResult.OK) //如果结果为"打开",选定文件
{
curFileName = opnDlg.FileName; //读取当前选中的文件名
//使用Image.FormFile创建对像
try
{
curBitmap = (Bitmap)Image.FromFile(curFileName);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message); //抛出异常
}
}
Invalidate(); //对窗体进行重新绘制,这钭强制执行paint事件处理程序
}
private void btnSave_Click(object sender, EventArgs e)
{
//如果没有创建图像,则退出
if (curBitmap == null)
{
return;
}
//调用SaveFileDialog
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Title = "保存为";
saveDlg.Overwrite