前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Windows Forms:在C#中将图像转换成灰度图

Windows Forms:在C#中将图像转换成灰度图

作者头像
ccf19881030
发布2021-03-02 15:14:18
1.5K0
发布2021-03-02 15:14:18
举报
文章被收录于专栏:ccf19881030的博客ccf19881030的博客

Windows Forms:在C#中将图像转换成灰度图

本文翻译自Windows Forms: Convert an image into grayscale in C# 这篇文章向你展示在C# Windows窗体应用程序中如何将图像转换成灰度图。 创建一个新的Windows窗体应用程序项目,然后创建一个允许你可以打开图像,然后将图像转换成黑白图像的简单的UI,如下图所示:

Convert Image
Convert Image

Open按钮添加单击事件处理,允许你选择一个图像文件,然后将图像显示到PictureBox控件中。 对应的代码如下所示:

代码语言:javascript
复制
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#中将图像转换成灰度图如下:

代码语言:javascript
复制
//  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按钮添加事件处理,允许你将图像转换成灰度图:

代码语言:javascript
复制
private void btnConvert_Click(object sender, EventArgs e)
        {
            picConvert.Image = MakeGrayscale((Bitmap)picOriginal.Image);
        }

下面是窗体的完整实现代码:

代码语言:javascript
复制
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);
        }
    }
}

lena图像素材下载

图像处理中经常使用到的Lena图片 下载地址: https://bellard.org/bpg/lena.html

http://www.lenna.org/

Lena的图片
Lena的图片

这个用于图像处理的Lena图像是用于图像处理中很好的研究素材,如下图所示:

http://www.lenna.org/
http://www.lenna.org/

程序效果

程序效果展示
程序效果展示

参考资料

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-02-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Windows Forms:在C#中将图像转换成灰度图
  • lena图像素材下载
  • 程序效果
  • 参考资料
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档