相关文章推荐
俊逸的萝卜  ·  Groupby,sum,reset ...·  1 年前    · 
逆袭的高山  ·  ffmpeg | 8路摄像头视频录制方案 - 知乎·  2 年前    · 
重感情的热水瓶  ·  WPS ...·  2 年前    · 
淡定的小刀  ·  React(三)状态值、样式、双向绑定、渲染 ...·  2 年前    · 
精明的铁链  ·  一个接口有多个实现类_51CTO博客_一个接 ...·  2 年前    · 
Code  ›  Windows Forms:在C#中将图像转换成灰度图开发者社区
bitmap 灰度图
https://cloud.tencent.com/developer/article/1795593
爱喝酒的葫芦
1 年前
ccf19881030

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

前往小程序,Get 更优 阅读体验!
立即前往
腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
工具
TVP
最新优惠活动
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
ccf19881030
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
社区首页 > 专栏 > Windows Forms:在C#中将图像转换成灰度图

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

作者头像
ccf19881030
发布 于 2021-03-02 15:14:18
1.5K 0
发布 于 2021-03-02 15:14:18
举报
文章被收录于专栏: 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);
 
推荐文章
俊逸的萝卜  ·  Groupby,sum,reset index &保持第一位-腾讯云开发者社区-腾讯云
1 年前
逆袭的高山  ·  ffmpeg | 8路摄像头视频录制方案 - 知乎
2 年前
重感情的热水瓶  ·  WPS EXCEL制作二级下拉框、动态下拉框、联动下拉框_一级下拉框动态二级跟着调整_学习要趁早z的博客-CSDN博客
2 年前
淡定的小刀  ·  React(三)状态值、样式、双向绑定、渲染(条件、列表)-阿里云开发者社区
2 年前
精明的铁链  ·  一个接口有多个实现类_51CTO博客_一个接口有多个实现类 autowired
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号