尝试读取所有像素并将其填充到3阵列(rgb)中,您可以在其中设置一个逻辑来替换您的颜色。

在C#中替换图像中的颜色

共 5 个回答
高赞
时间
活跃
0

0

找到了解决方法,这需要RGB <-> HSL转换(可以在此处找到HSL颜色的良好类)
1.获取代表您要替换的颜色的参考值(以hsl为单位)
2.获取目标颜色的hsl值
3.获取图像像素,并针对每个像素:
4.计算像素的hsl值,并将其替换为(pixelHsl / refHsl)* targetHsl
这为我完成了工作,感谢所有提供帮助的人
0

尝试这个:
Color color = Color.Black; //Your desired colour
byte r = color.R; //For Red colour
Bitmap bmp = new Bitmap(this.BackgroundImage);
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color gotColor = bmp.GetPixel(x, y);
gotColor = Color.FromArgb(r, gotColor.G, gotColor.B);
bmp.SetPixel(x, y, gotColor);
}
}
0

经过研究,我发现没有高效/顺畅的方法,所以我自己也没有,可以完全清除代码,但可以完成工作,虽然效率不高,但更流畅,可以让您设置公差。
public static Image ColorReplace(this Image inputImage, int tolerance, Color oldColor, Color NewColor)
{
Bitmap outputImage = new Bitmap(inputImage.Width, inputImage.Height);
Graphics G = Graphics.FromImage(outputImage);
G.DrawImage(inputImage, 0, 0);
for (Int32 y = 0; y < outputImage.Height; y++)
for (Int32 x = 0; x < outputImage.Width; x++)
{
Color PixelColor = outputImage.GetPixel(x, y);
if (PixelColor.R > oldColor.R - tolerance && PixelColor.R < oldColor.R + tolerance && PixelColor.G > oldColor.G - tolerance && PixelColor.G < oldColor.G + tolerance && PixelColor.B > oldColor.B - tolerance && PixelColor.B < oldColor.B + tolerance)
{
int RColorDiff = oldColor.R - PixelColor.R;
int GColorDiff = oldColor.G - PixelColor.G;
int BColorDiff = oldColor.B - PixelColor.B;
if (PixelColor.R > oldColor.R) RColorDiff = NewColor.R + RColorDiff;
else RColorDiff = NewColor.R - RColorDiff;
if (RColorDiff > 255) RColorDiff = 255;
if (RColorDiff < 0) RColorDiff = 0;
if (PixelColor.G > oldColor.G) GColorDiff = NewColor.G + GColorDiff;
else GColorDiff = NewColor.G - GColorDiff;
if (GColorDiff > 255) GColorDiff = 255;
if (GColorDiff < 0) GColorDiff = 0;
if (PixelColor.B > oldColor.B) BColorDiff = NewColor.B + BColorDiff;
else BColorDiff = NewColor.B - BColorDiff;
if (BColorDiff > 255) BColorDiff = 255;
if (BColorDiff < 0) BColorDiff = 0;
outputImage.SetPixel(x, y, Color.FromArgb(RColorDiff, GColorDiff, BColorDiff));
}
}
return outputImage;
}
0

有效替换颜色的一种方法是使用重映射表。在下面的示例中,在图片框内绘制图像。在Paint事件中,颜色Color.Black更改为Color.Blue:
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
using (Bitmap bmp = new Bitmap("myImage.png"))
{
// Set the image attribute's color mappings
ColorMap[] colorMap = new ColorMap[1];
colorMap[0] = new ColorMap();
colorMap[0].OldColor = Color.Black;
colorMap[0].NewColor = Color.Blue;
ImageAttributes attr = new ImageAttributes();
attr.SetRemapTable(colorMap);
// Draw using the color map
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
g.DrawImage(bmp, rect, 0, 0, rect.Width, rect.Height, GraphicsUnit.Pixel, attr);
}
}
详细信息: http : //msdn.microsoft.com/zh-cn/library/4b4dc1kz%28v=vs.110%29.aspx
新手导航
- 社区规范
- 提出问题
- 进行投票
- 个人资料
- 优化问题
- 回答问题
0
用C#替换图像某些部分的颜色而不影响其纹理的方式是什么?
您可以在这里看到结果的好例子
谢谢