Replace color in image in C #

How can color be replaced in C # for some parts of an image without affecting its texture?

You can see a good example of the result here.
Thanks

+6
source share
5 answers

Found a way to do this, this requires RGB ↔ HSL transforms (a 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 the target color
3. Get the pixels of the image and for each pixel:
4. calculate the hsl value of the pixel and replace it (pixelHsl / refHsl) * targetHsl

It helped me, thanks to everyone who helped.

+2
source

One way to effectively replace colors is to use a reassignment table. In the following example, the image is drawn inside the image field. In the Paint event, the color of Color.Black changes to 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 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); } } 

Additional information: http://msdn.microsoft.com/en-us/library/4b4dc1kz%28v=vs.110%29.aspx

+8
source

try the following:

 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); } } 
+3
source

Try to read all the pixels and fill them in array 3 (rgb), where you can set in alogrithm to replace your colors.

+2
source

After research, I did not find an effective / smoothe way to do this, so I do not do it myself, the code can be cleared by ALOT, but it does its job, is inefficient, but it is smoother and allows you to set the 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; } 
0
source

Source: https://habr.com/ru/post/911663/


All Articles