private void 灰度转换ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
//位图矩形,0, 0, curBitmap.Width, curBitmap.Height分别表示左上角x,y坐标,矩形宽度,高度。
Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);
//以可读写的方式锁定全部位图像素
System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);
//得到首地址
IntPtr ptr = bmpData.Scan0;
//定义被锁定的数组大小,由位图数据与未用空间组成
int bytes = bmpData.Stride*bmpData.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
//灰度化
double colorTemp = 0;
for(int i=0;i<bmpData.
24位bmp图像转成灰度图像
最新推荐文章于 2024-03-17 20:09:53 发布
该段代码实现了一个将24位位图转换为灰度图像的功能。通过锁定位图数据,遍历每个像素的RGB值,应用灰度转换公式,然后更新像素值,最后解锁位图并刷新显示。
摘要由CSDN通过智能技术生成