C# Programming > Drawing

C# Image Invert Colors

c# image invert

Image Invert

Inverting image colors in C# means flipping the values of each color in an image. Flipping values requires reading the pixels of a bitmap and writing back new RGB values. The .NET Framework offers different ways to modify an image.

We'll go over 3 ways to invert an image in C#, each with a different method, but all achieving the same result.

1. GDI+

The simplest way is to use GDI+ to access pixels and invert the image. The functions GetPixel and SetPixel are all we really need. Two nested for loops, one for the x dimension and one for the y dimension allows the application to process the entire .NET image.

Each individual color is inverted by subtracting the value of the red, blue, and green componets of the pixel from 255 (the max value). So for example, the inverted value of the R component would be 255 - Rvalue.

This method to invert an image is simple to write and makes the .NET source easy to read. However it is very slow. It works well for small images, but for large images it is way too slow.

2. ColorMatrix

Technically this is also GDI+, but instead of GetPixel and SetPixel, we rely on the Graphics class and the ColorMatrix class. The Graphics .NET class is a powerful and fast way to render graphical elements. This includes drawing existing images. What ColorMatrix does is fine tune how the image is rendered by the Graphics class.

Note that there are many examples on the web on image invert with ColorMatrix. However the way it is done in our application is the best since it produces correct results for all images. In fact the results are exactly the same as with method 1. The whole trick is to define the ColorMatrix as this:

new float[][]
{
   new float[] {-1, 0, 0, 0, 0},
   new float[] {0, -1, 0, 0, 0},
   new float[] {0, 0, -1, 0, 0},
   new float[] {0, 0, 0, 1, 0},
   new float[] {1, 1, 1, 0, 1}
}

Notice how the matrix inverts red, green, and blue values but does not invert alpha values. This is good or the image would end up completely transparent (thus no image would show up).

The advantage of this method is that, although the source code is not as clear as method 1, it is still simple to implement. Another benefit is that is a lot faster than method 1, especially for large images.

3. Unsafe Pixel Processing

The third way to invert an image in C# is to use unsafe pixel processing. This is by far the "messiest" way to invert an image, but it is also the fastest.

Unsafe pixel processing involves directly accessing the bytes of the image. It works very similar to method 1, and in fact the source code is structured the same way, there are just some extra steps to prepare access to the image bytes.

However since it is "unsafe" code, your .NET project must allow unsafe code to compile, a setting under Project Properties > Build (Allow unsafe code).

As mentioned earlier, this is the fastest of all three methods. It is a lot faster than method 1, and slightly faster than method 2.

Conclusion

The great thing about the three methods is that all of them yield the same C# image inverted. Deciding which one to use is personal choice and what it is best suited for the application.

Back to C# Article List