因为Bitmap,有一种MakeTransparent方法,有一种类似的方法可以将一种颜色改变为另一种颜色吗?
// This sets Color.White to transparent
Bitmap myBitmap = new Bitmap(sr.Stream);
myBitmap.MakeTransparent(System.Drawing.Color.White);
Run Code Online (Sandbox Code Playgroud)
有什么东西可以做这样的事吗?
Bitmap myBitmap = new Bitmap(sr.Stream);
myBitmap.ChangeColor(System.Drawing.Color.Black, System.Drawing.Color.Gray);
Run Code Online (Sandbox Code Playgroud)
Say*_*yse 10
通过对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);
}
}
Run Code Online (Sandbox Code Playgroud)
叫做 bmp.ChangeColour(0,128,0,0,0,0);
vol*_*ody -1
您可以使用SetPixel来实现:
private void ChangeColor(Bitmap s, System.Drawing.Color source, System.Drawing.Color target)
{
for (int x = 0; x < s.Width; x++)
{
for (int y = 0; y < s.Height; y++)
{
if (s.GetPixel(x, y) == source)
s.SetPixel(x, y, target);
}
}
}
Run Code Online (Sandbox Code Playgroud)
GetPixel 和 SetPixel 是 gdiplus.dll 函数 GdipBitmapGetPixel 和 GdipBitmapSetPixel 的包装器
评论:
根据位图的格式,GdipBitmapGetPixel 返回的值可能与 GdipBitmapSetPixel 设置的值不同。例如,如果您对像素格式为 32bppPARGB 的 Bitmap 对象调用 GdipBitmapSetPixel,则该像素的 RGB 分量将被预乘。由于舍入,后续调用 GdipBitmapGetPixel 可能会返回不同的值。另外,如果对颜色深度为每像素 16 位的 Bitmap 对象调用 GdipBitmapSetPixel,则在从 32 位转换为 16 位的过程中可能会丢失信息,并且对 GdipBitmapGetPixel 的后续调用可能会返回不同的值。
| 归档时间: |
|
| 查看次数: |
7839 次 |
| 最近记录: |