当前位置: 首页 > news >正文

C# 更改Bitmap图像色彩模式

方法一:直接修改RGB的值

首先将BitmapData扫描线上的所有像素复制到字节数组中,然后遍历数组并对每个像素的RGB值进行修改,最后将修改后的像素值复制回BitmapData。这个过程不会影响原始的Bitmap对象,但会改变锁定的位图区域的数据。当完成修改后,应调用UnlockBits()方法释放锁定的位图区域。
 

System.Drawing.Bitmap bitBufferRGB = new System.Drawing.Bitmap("彩色Bitmap图像.jpg");
System.Drawing.Imaging.BitmapData data = bitBufferRGB.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitBufferRGB.Size),
System.Drawing.Imaging.ImageLockMode.ReadWrite, bitBufferRGB.PixelFormat);//获取内存
IntPtr pData = data.Scan0;
int bytes = data.Stride * bitBufferRGB.Height;
byte[] rgbValues = new byte[bytes];// Copy the RGB values into the array.
System.Runtime.InteropServices.Marshal.Copy(pData, rgbValues, 0, bytes);for (int y = 0; y < bitBufferRGB.Height; y++)
{for (int x = 0; x < bitBufferRGB.Width; x++){// 获取像素(x, y)在数组中的索引。int index = y * data.Stride + x * 3;// 修改RGB值。rgbValues[index] = (byte)(rgbValues[index] * 0.9); // 修改红色分量rgbValues[index + 1] = (byte)(rgbValues[index + 1] * 0.7); // 修改绿色分量rgbValues[index + 2] = (byte)(rgbValues[index + 2] * 0.9); // 修改蓝色分量}
}// Copy the modified RGB values back to the bitmap.
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, pData, bytes);//解锁及释放资源
bitBufferRGB.UnlockBits(data);
bitBufferRGB.Dispose();

方法二:更换RGB的值位置

更换R和B的位置

System.Drawing.Bitmap bitBufferRGB = new System.Drawing.Bitmap("彩色Bitmap图像.jpg");
System.Drawing.Imaging.BitmapData data = bitBufferRGB.LockBits(
new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitBufferRGB.Size),
System.Drawing.Imaging.ImageLockMode.ReadWrite, bitBufferRGB.PixelFormat);//获取内存
IntPtr pData = data.Scan0;
int bytes = data.Stride * bitBufferRGB.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(pData, rgbValues, 0, bytes);for (int i = 0; i < height; i++)
{for (int ji = 0; ji < width; ji++){int index = i * width + ji;// 每个像素占用三个字节// 红色字节rgbValues[index * 3] = System.Runtime.InteropServices.Marshal.ReadByte(pData, index * 3 + 2);// 绿色字节rgbValues[index * 3 + 1] = System.Runtime.InteropServices.Marshal.ReadByte(pData, index * 3 + 1);// 蓝色字节rgbValues[index * 3 + 2] = System.Runtime.InteropServices.Marshal.ReadByte(pData, index * 3);}
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, pData, bytes);//解锁及释放资源
bitBufferRGB.UnlockBits(data);
bitBufferRGB.Dispose();

 

 

 

 

 

http://www.lryc.cn/news/285756.html

相关文章:

  • 5.2 基于深度学习和先验状态的实时指纹室内定位
  • AIGC时代高效阅读论文实操
  • 对网站进行打点(不要有主动扫描行为)
  • 502. IPO(贪心算法+优先队列/堆)
  • 设计模式篇---中介者模式
  • 双端Diff算法
  • react+antd,Table表头文字颜色设置
  • 2024年1月18日Arxiv最热NLP大模型论文:Large Language Models Are Neurosymbolic Reasoners
  • 服务限流实现方案
  • 【RTOS】快速体验FreeRTOS所有常用API(1)工程创建
  • Red Hat Enterprise Linux 8.9 安装图解
  • vcruntime140.dll文件修复的几种常见解决办法,vcruntime140.dll丢失的原因
  • SpringCloud Alibaba 深入源码 - Nacos 分级存储模型、支撑百万服务注册压力、解决并发读写问题(CopyOnWrite)
  • 算法训练营Day45
  • 【Redis漏洞利用总结】
  • SPI 动态服务发现机制
  • 【C++进阶07】哈希表and哈希桶
  • Go 语言实现冒泡排序算法的简单示例
  • JAVA 学习 面试(五)IO篇
  • vue3相比vue2的效率提升
  • web terminal - 如何在mac os上运行gotty
  • 机械设计-哈工大课程学习-螺纹连接
  • ai绘画|stable diffusion的发展史!简短易懂!!!
  • 水塘抽样算法
  • easyui渲染隐藏域<input type=“hidden“ />为textbox可作为分割条使用
  • 100天精通Python(实用脚本篇)——第113天:基于Tesseract-OCR实现OCR图片文字识别实战
  • Go七天实现RPC
  • Elasticsearch:和 LIamaIndex 的集成
  • QT基础篇(14)QT操作office实例
  • 重拾计网-第四弹 计算机网络性能指标