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();

 

 

 

 

 

相关推荐

  1. C# 更改Bitmap图像色彩模式

    2024-01-22 23:38:02       45 阅读
  2. 图像色彩还原算法

    2024-01-22 23:38:02       57 阅读
  3. c# Bitmap

    2024-01-22 23:38:02       24 阅读
  4. Android Bitmap 图片裁剪

    2024-01-22 23:38:02       49 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-22 23:38:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-22 23:38:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-22 23:38:02       87 阅读
  4. Python语言-面向对象

    2024-01-22 23:38:02       96 阅读

热门阅读

  1. C 练习实例37 - 排序

    2024-01-22 23:38:02       52 阅读
  2. 14.任务管理系统

    2024-01-22 23:38:02       43 阅读
  3. 我的创作纪念日

    2024-01-22 23:38:02       50 阅读
  4. 2024.1.18力扣每日一题——拿出最少数目的魔法豆

    2024-01-22 23:38:02       63 阅读
  5. 前端笔试题(九)——请使用jQuery实现Ajax请求

    2024-01-22 23:38:02       49 阅读
  6. day13打卡

    2024-01-22 23:38:02       61 阅读
  7. String 字符串类和编码 以及StringBuilder StringBuffer

    2024-01-22 23:38:02       47 阅读
  8. 猫咪与Git 解决git clone 443问题

    2024-01-22 23:38:02       53 阅读
  9. Leetcode 3012. Minimize Length of Array Using Operations

    2024-01-22 23:38:02       61 阅读