在 C# 中替换图像中的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9871262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
replace color in an image in c#
提问by Oren
采纳答案by Oren
Found the way to do that, this requires RGB<->HSL conversions (good class for HSL color can be found here)
1. Get a reference value (in hsl) representing the color you want to replace
2. Get the hsl value for your target color
3. Get image pixels and for each pixel:
4. calculate the hsl value of the pixel, and replace it with (pixelHsl / refHsl) * targetHsl
找到了这样做的方法,这需要 RGB<->HSL 转换(可以在此处找到有关 HSL 颜色的好类)
1. 获取表示要替换的颜色的参考值(以 hsl 表示)
2. 获取 hsl 值您的目标颜色
3. 获取图像像素和每个像素:
4. 计算像素的 hsl 值,并将其替换为 (pixelHsl / refHsl) * targetHsl
This did the job for me, thanks for all who helped
这对我有用,感谢所有帮助过我的人
回答by Ali Issa
try this:
尝试这个:
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);
}
}
回答by jorne
Try to read out al the pixels and stuff them in an 3 array's (rgb) there you can set in a alogrithm to replace your colors.
尝试读出所有像素并将它们填充到 3 个数组 (rgb) 中,您可以在其中设置一个算法来替换您的颜色。
回答by jcibar
One way to efficiently replace a color is to use a remap table. In the following example, an image is drawn inside a picture box. In the Paint event, the color Color.Black is changed to Color.Blue:
有效替换颜色的一种方法是使用重映射表。在以下示例中,在图片框内绘制图像。在 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);
}
}
More information: http://msdn.microsoft.com/en-us/library/4b4dc1kz%28v=vs.110%29.aspx
更多信息:http: //msdn.microsoft.com/en-us/library/4b4dc1kz%28v=vs.110%29.aspx
回答by Ricky Divjakovski
Upon doing research i found no efficient/smoothe way of doing this, so i dont it myself, the code could be cleaned up ALOT but it gets the job done, its not efficient but it is smoother and allows you to set a tolerance.
在进行研究后,我发现没有有效/平滑的方法来做到这一点,所以我自己不这样做,代码可以被清理很多,但它可以完成工作,它效率不高但更流畅,并允许您设置容差。
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;
}

