How to Invert an Image

Will be posting article on how to create simple application, with full working code in C#
And welcome to anyone who want to post their own article
Post Reply
User avatar

Topic author
Superl
Site Admin
Site Admin
Posts: 1267
Joined: Sat Apr 16, 2011 7:49 am
Location: Montreal, Canada
Contact:

How to Invert an Image

#2279

Post by Superl » Sat Dec 19, 2015 6:08 am

Invert an Image Introduction:
Invert an image is like to view the negative of an image. With this method you will be able to control the degree of transparency with the alpha variable
  1. /// <summary>
  2. /// Inverts an Image with the option to Invert it's Transparency.
  3. /// </summary>
  4. /// <param name="bitmap">The Bitmap Image you want to Invert.</param>
  5. /// <param name="InvertAlpha">True to Invert it's Transparency. False to Invert the Image Only.</param>
  6. /// <returns>An Inverted Image</returns>
  7. public Bitmap Invert(Bitmap bitmap)
  8. {
  9.        //X Axis
  10.        int x;
  11.        //Y Axis
  12.        int y;
  13.  
  14.        //For the Width
  15.        for (x = 0; x <= bitmap.Width - 1; x++)
  16.        {
  17.               //For the Height
  18.               for (y = 0; y <= bitmap.Height - 1; y += 1)
  19.               {
  20.                      //The Old Color to Replace
  21.                      Color oldColor = bitmap.GetPixel(x, y);
  22.                      //The New Color to Replace the Old Color
  23.                      Color newColor;
  24.  
  25.                      //Set the Color for newColor
  26.                      newColor = Color.FromArgb(oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 - oldColor.B);
  27.  
  28.                      //Replace the Old Color with the New Color
  29.                      bitmap.SetPixel(x, y, newColor);
  30.               }
  31.        }
  32.       //Return the Inverted Bitmap
  33.        return bitmap;
  34. }
  35.  
  36. /// <summary>
  37. /// Inverts an Image with the option to Invert it's Transparency.
  38. /// </summary>
  39. /// <param name="bitmap">The Bitmap Image you want to Invert.</param>
  40. /// <param name="InvertAlpha">True to Invert it's Transparency. False to Invert the Image Only.</param>
  41. /// <returns>An Inverted Image</returns>
  42. public Bitmap Invert(Bitmap bitmap, bool InvertAlpha)
  43. {
  44.        //X Axis
  45.        int x;
  46.        //Y Axis
  47.        int y;
  48.        //For the Width
  49.        for (x = 0; x <= bitmap.Width - 1; x++)
  50.        {
  51.               //For the Height
  52.               for (y = 0; y <= bitmap.Height - 1; y += 1)
  53.               {
  54.                      //The Old Color to Replace
  55.                      Color oldColor = bitmap.GetPixel(x, y);
  56.                      //The New Color to Replace the Old Color
  57.                      Color newColor;
  58.                      //If the user has chosen to Invert the Transparency
  59.                      if (InvertAlpha)
  60.                      {
  61.                             //Set the Color for newColor
  62.                             newColor = Color.FromArgb(255 - oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 -                                           oldColor.B);
  63.                      }
  64.                      else
  65.                      {
  66.                             //Set the Color for newColor
  67.                             newColor = Color.FromArgb(oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 -                                                  oldColor.B);
  68.                      }
  69.                      //Replace the Old Color with the New Color
  70.                      bitmap.SetPixel(x, y, newColor);
  71.               }
  72.        }
  73.        //Return the Inverted Bitmap
  74.        return bitmap;


Come and say hello in here
Any donation will help click here please.

Have a nice day :103:

Post Reply

Return to “Coding Forum”

Who is online

Users browsing this forum: No registered users and 3 guests