Applying a color filter to a Bitmap object

I found this code on how to apply a color filter to a Bitmap object in C #. The problem with this is using unsafe code for this. Is there a manageable, safe way to do the same? I know that I can use the AForge.NET library or something similar, but I hope there is an easy way to apply a color filter. All I need is a simple color swap, replacing white pixels with yellow. Any suggestions?

+4
source share
1 answer

You can always use the safe GetPixel and SetPixel methods, but they are slow to use on many pixels, which causes the unsafe method to use pointers in raster memory. An unsafe way to do this.

If your bitmap is very small and you still don't care about performance, use the GetPixel and SetPixel methods.

private void ReplaceColor(Bitmap bitmap, Color originalColor, Color replacementColor) { for (var y = 0; y < bitmap.Height; y++) { for (var x = 0; x < bitmap.Width; x++) { if (bitmap.GetPixel(x, y) == originalColor) { bitmap.SetPixel(x, y, replacementColor); } } } } private unsafe void ReplaceColorUnsafe(Bitmap bitmap, byte[] originalColor, byte[] replacementColor) { if (originalColor.Length != replacementColor.Length) { throw new ArgumentException("Original and Replacement arguments are in different pixel formats."); } if (originalColor.SequenceEqual(replacementColor)) { return; } var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.ReadWrite, bitmap.PixelFormat); var bpp = Image.GetPixelFormatSize(data.PixelFormat); if (originalColor.Length != bpp) { throw new ArgumentException("Original and Replacement arguments and the bitmap are in different pixel format."); } var start = (byte*)data.Scan0; var end = start + data.Stride; for (var px = start; px < end; px += bpp) { var match = true; for (var bit = 0; bit < bpp; bit++) { if (px[bit] != originalColor[bit]) { match = false; break; } } if (!match) { continue; } for (var bit = 0; bit < bpp; bit++) { px[bit] = replacementColor[bit]; } } bitmap.UnlockBits(data); } 

Using:

  this.ReplaceColor(myBitmap, Color.White, Color.Yellow); // SLOW 

OR

  var orgRGB = new byte[] { 255, 255, 255 }; // White (in RGB format) var repRGB = new byte[] { 255, 255, 0 }; // Yellow (in RGB format) var orgARGB = new byte[] { 255, 255, 255, 255 }; // White (in ARGB format) var repARGB = new byte[] { 255, 255, 255, 0 }; // Yellow (in ARGB format) var orgRGBA = new byte[] { 255, 255, 255, 255 }; // White (in RGBA format) var repRGBA = new byte[] { 255, 255, 0, 255 }; // Yellow (in RGBA format) var orgBytes = orgRGB; // or ARGB or RGBA, depending on bitmap pixel format var repBytes = repRGB; // or ARGB or RGBA, depending on bitmap pixel format this.ReplaceColorUnsafe(myBitmap, orgBytes, repBytes); // FAST 
+4
source

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


All Articles