C#实现把彩 * 片灰度化代码分享
作者:junjie 时间:2022-04-21 18:11:33
彩 * 片转为灰度图的公式如下:
gray(i,j) = 0.299 * Red(i,j)+0.587*Green(i,j)+0.114*Blue(i,j)
其中gray(i,j) 为转化后的灰度值 (i,j)为像素点的位置。
源代码如下:
public static Bitmap ChangeGray(Bitmap b)
{
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
int stride = bmData.Stride; // 扫描的宽度
unsafe
{
byte* p = (byte*)bmData.Scan0.ToPointer(); // 获取图像首地址
int nOffset = stride - b.Width * 3; // 实际宽度与系统宽度的距离
byte red, green, blue;
for (int y = 0; y < b.Height; ++y)
{
for (int x = 0; x < b.Width; ++x)
{
blue = p[0];
green = p[1];
red = p[2];
p[0] = p[1] = p[2] = (byte)(.299 * red + .587 * green + .114 * blue); // 转换公式
p += 3; // 跳过3个字节处理下个像素点
}
p += nOffset; // 加上间隔
}
}
b.UnlockBits(bmData); // 解锁
return b;
}
标签:C#,图片,灰度化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决idea check out 切换分支时找不到需要的分支问题
2023-04-04 09:12:57
![](https://img.aspxhome.com/file/2023/3/65743_0s.jpg)
如何使用ByteArrayOutputStream下载文件
2021-07-29 11:32:45
Java IO之序列化与反序列化详解
2023-05-21 14:59:14
![](https://img.aspxhome.com/file/2023/6/80156_0s.jpg)
springboot如何去除debug日志
2023-02-14 08:43:37
![](https://img.aspxhome.com/file/2023/9/92679_0s.png)
Java自定义实现链队列详解
2023-06-22 12:47:31
![](https://img.aspxhome.com/file/2023/6/57426_0s.png)
C#数据结构之单链表(LinkList)实例详解
2021-09-15 21:40:14
![](https://img.aspxhome.com/file/2023/3/100083_0s.png)
Java中输入与输出的方法总结
2021-06-07 22:02:15
![](https://img.aspxhome.com/file/2023/4/63854_0s.png)
深入解析kafka 架构原理
2023-11-18 13:40:14
![](https://img.aspxhome.com/file/2023/7/75457_0s.jpg)
Java和C#输入输出流的方法(详解)
2022-06-24 09:21:02
10种简单的Java性能优化
2023-06-20 20:43:41
![](https://img.aspxhome.com/file/2023/6/57526_0s.png)
unity3d实现七天签到功能
2023-01-05 08:57:21
SpringSceurity实现短信验证码登陆
2023-06-23 00:37:35
![](https://img.aspxhome.com/file/2023/2/77102_0s.jpg)
android保存Bitmap图片到指定文件夹示例
2022-10-11 22:59:14
浅谈java的接口和C++虚类的相同和不同之处
2023-08-05 12:01:51
浅谈Maven的build生命周期和常用plugin
2022-11-03 01:47:52
![](https://img.aspxhome.com/file/2023/1/75441_0s.jpg)
Unity3D开发实战之五子棋游戏
2023-06-01 12:25:56
![](https://img.aspxhome.com/file/2023/8/100158_0s.gif)
Android入门之Handler的使用教程详解
2022-06-17 04:03:22
![](https://img.aspxhome.com/file/2023/7/87307_0s.png)
Java中过滤器 (Filter) 和 拦截器 (Interceptor)的使用
2023-07-07 00:20:28
![](https://img.aspxhome.com/file/2023/3/63453_0s.png)
java内存模型jvm虚拟机简要分析
2022-09-08 09:29:34
![](https://img.aspxhome.com/file/2023/3/101933_0s.jpg)
C#多线程基础知识汇总
2023-03-06 08:35:47
![](https://img.aspxhome.com/file/2023/3/86963_0s.png)