gpt4 book ai didi

c# - 您可以将位图图像中的一种颜色更改为另一种颜色吗?

转载 作者:太空狗 更新时间:2023-10-29 20:17:21 25 4
gpt4 key购买 nike

对于Bitmap,有一个MakeTransparent方法,有没有类似的方法可以将一种颜色更改为另一种颜色?

// This sets Color.White to transparent
Bitmap myBitmap = new Bitmap(sr.Stream);
myBitmap.MakeTransparent(System.Drawing.Color.White);

有没有什么东西可以做这样的事情?

Bitmap myBitmap = new Bitmap(sr.Stream);
myBitmap.ChangeColor(System.Drawing.Color.Black, System.Drawing.Color.Gray);

最佳答案

出于对 Yorye Nathan 评论的好奇,这是我通过修改 http://msdn.microsoft.com/en-GB/library/ms229672(v=vs.90).aspx 创建的扩展。 .

它可以将位图中的所有像素从一种颜色变为另一种颜色。

public static class BitmapExt
{
public static void ChangeColour(this Bitmap bmp, byte inColourR, byte inColourG, byte inColourB, byte outColourR, byte outColourG, byte outColourB)
{
// Specify a pixel format.
PixelFormat pxf = PixelFormat.Format24bppRgb;

// Lock the bitmap's bits.
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData =
bmp.LockBits(rect, ImageLockMode.ReadWrite,
pxf);

// Get the address of the first line.
IntPtr ptr = bmpData.Scan0;

// Declare an array to hold the bytes of the bitmap.
// int numBytes = bmp.Width * bmp.Height * 3;
int numBytes = bmpData.Stride * bmp.Height;
byte[] rgbValues = new byte[numBytes];

// Copy the RGB values into the array.
Marshal.Copy(ptr, rgbValues, 0, numBytes);

// Manipulate the bitmap
for (int counter = 0; counter < rgbValues.Length; counter += 3)
{
if (rgbValues[counter] == inColourR &&
rgbValues[counter + 1] == inColourG &&
rgbValues[counter + 2] == inColourB)

{
rgbValues[counter] = outColourR;
rgbValues[counter + 1] = outColourG;
rgbValues[counter + 2] = outColourB;
}
}

// Copy the RGB values back to the bitmap
Marshal.Copy(rgbValues, 0, ptr, numBytes);

// Unlock the bits.
bmp.UnlockBits(bmpData);
}
}

bmp.ChangeColour(0,128,0,0,0,0); 调用

关于c# - 您可以将位图图像中的一种颜色更改为另一种颜色吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16346212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com