Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Creating Negative Images using C#


This is another image processing post. In this post, I'm going to show you some basic code to create negative images. Generally speaking a negative image isn't that useful. However it does have the ability to help show differences within the image and plus it just looks cool. Basically what we're doing is a logical not (just flipping some bits). So if this were a 1bit image we'd just be flipping the 1s to 0s and 0s to 1s. However no one uses a 1bit image and instead we're using 24/32bit images (or 16 or 48 or 8...). So let's see what we need to do:

   1: /// <summary>
   2: /// gets the negative of the image
   3: /// </summary>
   4: /// <param name="OriginalImage">Image to manipulate</param>
   5: /// <returns>A bitmap image</returns>
   6: public static Bitmap Negative(Bitmap OriginalImage)
   7: {
   8:     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
   9:     BitmapData NewData = Image.LockImage(NewBitmap);
  10:     BitmapData OldData = Image.LockImage(OriginalImage);
  11:     int NewPixelSize = Image.GetPixelSize(NewData);
  12:     int OldPixelSize = Image.GetPixelSize(OldData);
  13:     for (int x = 0; x < NewBitmap.Width; ++x)
  14:     {
  15:         for (int y = 0; y < NewBitmap.Height; ++y)
  16:         {
  17:             Color CurrentPixel = Image.GetPixel(OldData, x, y, OldPixelSize);
  18:             Color TempValue = Color.FromArgb(255 - CurrentPixel.R, 255 - CurrentPixel.G, 255 - CurrentPixel.B);
  19:             Image.SetPixel(NewData, x, y, TempValue, NewPixelSize);
  20:         }
  21:     }
  22:     Image.UnlockImage(NewBitmap, NewData);
  23:     Image.UnlockImage(OriginalImage, OldData);
  24:     return NewBitmap;
  25: }

The code above uses a couple of functions from my utility library, specifically the Image.LockImage, UnlockImage, etc. functions. These functions are there to speed up things but can be removed/replaced with GetPixel/SetPixel functions that are built in to .Net. Anyway, if you look at the loop, all we're doing is taking the current pixel value and subtracting that from 255 for red, green, and blue values. So instead of just flipping bits we need to subtract the current pixel value from 255. That's all there is to it really. Anyway, I hope this helps you out. Give it a try, leave feedback, and happy coding.