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#

Anding Two Images Together Using C#


So first I'm stuck working on SharePoint site management (which I'm going to be doing for a little while) and then I become sick with something that seems to be taking out everyone around here. However I'm now back doing some coding (in my spare time anyway). And since my update to the ORM isn't ready yet, I'm going to hand out some more image manipulation code.

Today I'm going to show you how to and two images together. And, Or, Xor, etc. are logical operations that, as programmers, you've seen about 10,000 times. They can also be used in image processing, causing various effects. All of them are extremely simple, so let's look at the basic code:

   1: /// <summary>
   2: /// ands two images
   3: /// </summary>
   4: /// <param name="Image1">Image to manipulate</param>
   5: /// <param name="Image2">Image to manipulate</param>
   6: /// <returns>A bitmap image</returns>
   7: public static Bitmap And(Bitmap Image1, Bitmap Image2)
   8: {
   9:     Bitmap NewBitmap = new Bitmap(Image1.Width, Image1.Height);
  10:     BitmapData NewData = Image.LockImage(NewBitmap);
  11:     BitmapData OldData1 = Image.LockImage(Image1);
  12:     BitmapData OldData2 = Image.LockImage(Image2);
  13:     int NewPixelSize = Image.GetPixelSize(NewData);
  14:     int OldPixelSize1 = Image.GetPixelSize(OldData1);
  15:     int OldPixelSize2 = Image.GetPixelSize(OldData2);
  16:     for (int x = 0; x < NewBitmap.Width; ++x)
  17:     {
  18:         for (int y = 0; y < NewBitmap.Height; ++y)
  19:         {
  20:             Color Pixel1 = Image.GetPixel(OldData1, x, y, OldPixelSize1);
  21:             Color Pixel2 = Image.GetPixel(OldData2, x, y, OldPixelSize2);
  22:             Image.SetPixel(NewData, x, y,
  23:                 Color.FromArgb(Pixel1.R & Pixel2.R,
  24:                     Pixel1.G & Pixel2.G,
  25:                     Pixel1.B & Pixel2.B),
  26:                 NewPixelSize);
  27:         }
  28:     }
  29:     Image.UnlockImage(NewBitmap, NewData);
  30:     Image.UnlockImage(Image1, OldData1);
  31:     Image.UnlockImage(Image2, OldData2);
  32:     return NewBitmap;
  33: }

The code above uses a couple of functions from my utility library, specifically Image.LockImage, UnlockImage, etc. Most of which can be removed or replaced with GetPixel/SetPixel functions. As you can see, all that it takes in is two images (and it makes the assumption that they're the same size). It then just does a logical and of the red, green, and blue values for the two pixels. You can easily substitute a ^ or a | instead of the &. That's all there is to it really. So try it out, leave feedback, and happy coding.