C# 更改Bitmap图像色彩模式

方法一:直接修改RGB的值

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

cs 复制代码
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的位置

cs 复制代码
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();
相关推荐
风雅颂FYS1 小时前
C# 经纬度坐标的精度及WGS84(谷歌)、GCJ02(高德)、BD09(百度)坐标相互转换(含高精度转换)
百度·c#
姜行运1 小时前
每日算法(双指针算法)(Day 1)
c++·算法·c#
vil du4 小时前
c# 反射及优缺点
c#
三天不学习6 小时前
C# + Python混合开发实战:优势互补构建高效应用
开发语言·python·c#
谢道韫6666 小时前
37-串联所有单词的子串
开发语言·算法·c#
CodeCraft Studio8 小时前
PDF处理控件Aspose.PDF指南:使用 C# 从 PDF 文档中删除页面
前端·pdf·c#
全栈小58 小时前
【C#】Html转Pdf,Spire和iTextSharp结合,.net framework 4.8
pdf·c#·html
唐青枫9 小时前
dotnet 值拷贝、浅拷贝、深拷贝详解
c#·.net
电商api接口开发16 小时前
如何在C#中使用LINQ对数据库进行查询操作?
数据库·c#·linq
搬砖工程师Cola17 小时前
<C#>在 .NET 开发中,依赖注入, 注册一个接口的多个实现
开发语言·c#·.net