相关文章推荐
淡定的钥匙  ·  武松_百度百科·  7 月前    · 
善良的四季豆  ·  小刀锋利最精彩的小说:不是《无疆》,而是被封 ...·  8 月前    · 
曾经爱过的西装  ·  自行车| NANOALLOY™ | 东丽·  8 月前    · 
帅呆的苦咖啡  ·  雲科工設X ...·  9 月前    · 
面冷心慈的牛腩  ·  卢克索南部发现早期象形文字铭文-埃及国家信息 ...·  9 月前    · 
小百科  ›  Windows Forms:在C#中将图像转换成灰度图开发者社区
bitmap 灰度图
爱喝酒的葫芦
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);
 
推荐文章
淡定的钥匙  ·  武松_百度百科
7 月前
善良的四季豆  ·  小刀锋利最精彩的小说:不是《无疆》,而是被封为神作的第三本!_ ...
8 月前
曾经爱过的西装  ·  自行车| NANOALLOY™ | 东丽
8 月前
帅呆的苦咖啡  ·  雲科工設X 日本GK設計集團「國際交通工具設計營」攜手共創未來的 ...
9 月前
面冷心慈的牛腩  ·  卢克索南部发现早期象形文字铭文-埃及国家信息服务中心
9 月前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
小百科 - 百科知识指南
© 2024 ~ 沪ICP备11025650号