Windows Forms:在C#中将图像转换成灰度图
Windows Forms:在C#中将图像转换成灰度图
本文翻译自 Windows Forms: Convert an image into grayscale in C# 这篇文章向你展示在C# Windows窗体应用程序中如何将图像转换成灰度图。 创建一个新的Windows窗体应用程序项目,然后创建一个允许你可以打开图像,然后将图像转换成黑白图像的简单的UI,如下图所示:
为
Open
按钮添加单击事件处理,允许你选择一个图像文件,然后将图像显示到
PictureBox
控件中。
对应的代码如下所示:
private void btnOpen_Click(object sender, EventArgs e)
using (OpenFileDialog openFileDlg = new OpenFileDialog() { Filter = "Images|*.jpg" })
if (openFileDlg.ShowDialog() == DialogResult.OK)
picOriginal.Image = Image.FromFile(openFileDlg.FileName);
}
下一步,创建一个
MakeGrayscale
方法允许你在C#中将图像转换成灰度图如下:
// convert an image into grayscale in c#
public Bitmap MakeGrayscale(Bitmap original)
// You need to create a new bitmap with size the same as image original,
// then create a color matrix and convert a color image to grayscale with C#.
Bitmap newBmp = new Bitmap(original.Width, original.Height);
Graphics g = Graphics.FromImage(newBmp);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
ImageAttributes img = new ImageAttributes();
img.SetColorMatrix(colorMatrix);
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, img);
g.Dispose();
return newBmp;
}
你需要创建一个和原图像一样大小的位图,然后创建一个颜色矩阵,并在C#中将彩色图转换成灰度图。
最后,为
Convert
按钮添加事件处理,允许你将图像转换成灰度图:
private void btnConvert_Click(object sender, EventArgs e)
picConvert.Image = MakeGrayscale((Bitmap)picOriginal.Image);
}
下面是窗体的完整实现代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConvertImageIntoGrayscale
public partial class Form1 : Form
public Form1()
InitializeComponent();
// convert an image into grayscale in c#
public Bitmap MakeGrayscale(Bitmap original)
// You need to create a new bitmap with size the same as image original,
// then create a color matrix and convert a color image to grayscale with C#.
Bitmap newBmp = new Bitmap(original.Width, original.Height);
Graphics g = Graphics.FromImage(newBmp);
ColorMatrix colorMatrix = new ColorMatrix(
new float[][]
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
ImageAttributes img = new ImageAttributes();
img.SetColorMatrix(colorMatrix);
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height), 0, 0, original.Width, original.Height, GraphicsUnit.Pixel, img);
g.Dispose();
return newBmp;
private void btnOpen_Click(object sender, EventArgs e)
using (OpenFileDialog openFileDlg = new OpenFileDialog() { Filter = "Images|*.jpg" })
if (openFileDlg.ShowDialog() == DialogResult.OK)
picOriginal.Image = Image.FromFile(openFileDlg.FileName);
private void btnConvert_Click(object sender, EventArgs e)
picConvert.Image = MakeGrayscale((Bitmap)picOriginal.Image);