【C# 】更改图像(dicom、Bitmap)像素的色彩模式

17 篇文章 3 订阅
本文详细介绍了如何使用C#的System.Drawing库,分别通过直接修改RGB值和交换R和B位置的方式,对Bitmap图像进行像素级别的操作,包括数据复制、修改和释放锁定的位图资源。
摘要由CSDN通过智能技术生成

一、修改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();

二、修改dicom像素的RGB值位置

C# 使用fo-dicom操作dicom文件-CSDN博客

首先读取dicom的像素数据,然后通过循环除了像素中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();

  • 13
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangnaisheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值