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#

Using Sin to Get What You Want... In C#...


I wanted at least one of my titles to be creative.  It's not, but at least I tried.  Anyway, today I wanted to share some simple code with you to do sine waves on an image (taking an image and making it look all wavy). There are a number of reasons you may want to use this code, but the one that jumps out at me right now is a Captcha control (since that's why I created the code in the first place, and I'll be putting that out here in a bit as well). But then again maybe you like simply looking at screwed up pictures... Whatever the reason, here is the code:

   1: /// <summary>
   2: /// Does a "wave" effect on the image
   3: /// </summary>
   4: /// <param name="OriginalImage">Image to manipulate</param>
   5: /// <param name="Amplitude">Amplitude of the sine wave</param>
   6: /// <param name="Frequency">Frequency of the sine wave</param>
   7: /// <param name="XDirection">Determines if this should be done in the X direction</param>
   8: /// <param name="YDirection">Determines if this should be done in the Y direction</param>
   9: /// <returns>A bitmap which has been modified</returns>
  10: public static Bitmap SinWave(Bitmap OriginalImage, float Amplitude, float Frequency, bool XDirection, bool YDirection)
  11: {
  12:     Bitmap NewBitmap = new Bitmap(OriginalImage.Width, OriginalImage.Height);
  13:     BitmapData NewData = Image.LockImage(NewBitmap);
  14:     BitmapData OldData = Image.LockImage(OriginalImage);
  15:     int NewPixelSize = Image.GetPixelSize(NewData);
  16:     int OldPixelSize = Image.GetPixelSize(OldData);
  17:     for (int x = 0; x < NewBitmap.Width; ++x)
  18:     {
  19:         for (int y = 0; y < NewBitmap.Height; ++y)
  20:         {
  21:             double Value1 = 0;
  22:             double Value2 = 0;
  23:             if (YDirection)
  24:                 Value1 = System.Math.Sin(((x * Frequency) * System.Math.PI) / 180.0d) * Amplitude;
  25:             if (XDirection)
  26:                 Value2 = System.Math.Sin(((y * Frequency) * System.Math.PI) / 180.0d) * Amplitude;
  27:             Value1 = y - (int)Value1;
  28:             Value2 = x - (int)Value2;
  29:             while (Value1 < 0)
  30:                 Value1 += NewBitmap.Height;
  31:             while (Value2 < 0)
  32:                 Value2 += NewBitmap.Width;
  33:             while (Value1 >= NewBitmap.Height)
  34:                 Value1 -= NewBitmap.Height;
  35:             while (Value2 >= NewBitmap.Width)
  36:                 Value2 -= NewBitmap.Width;
  37:             Image.SetPixel(NewData, x, y,
  38:                 Image.GetPixel(OldData, (int)Value2, (int)Value1, OldPixelSize),
  39:                 NewPixelSize);
  40:         }
  41:     }
  42:     Image.UnlockImage(NewBitmap, NewData);
  43:     Image.UnlockImage(OriginalImage, OldData);
  44:     return NewBitmap;
  45: }

There isn't much to the code, it simply takes each pixel from the input image and moves it based on a sine wave. You can decide whether you want to do this in the x direction, y direction, or both. Also you can decide on the frequency and amplitude of the sine wave. The frequency determines how wavy it is while amplitude determines how big the wave is.  The code takes care of everything else. There is one thing to note though, this code uses a couple of functions from my utility library to lock/unlock the images to speed things up a bit. You can simply remove those parts and change the SetPixel/GetPixel parts to the built in functions. Anyway, hopefully this helps out someone. So try it out, leave feedback, and happy coding.