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
Man of action
Man of action
Posts: 1331
Joined: Sat Apr 16, 2011 7:49 am
12
Location: Montreal, Canada
Contact:

How to Invert an Image

#2279

Post by Superl »

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

Code: Select all

/// <summary>
/// Inverts an Image with the option to Invert it's Transparency.
/// </summary>
/// <param name="bitmap">The Bitmap Image you want to Invert.</param>
/// <param name="InvertAlpha">True to Invert it's Transparency. False to Invert the Image Only.</param>
/// <returns>An Inverted Image</returns>
public Bitmap Invert(Bitmap bitmap)
{
//X Axis
int x;
//Y Axis
int y;

//For the Width
for (x = 0; x <= bitmap.Width - 1; x++)
{
//For the Height
for (y = 0; y <= bitmap.Height - 1; y += 1)
{
//The Old Color to Replace
Color oldColor = bitmap.GetPixel(x, y);
//The New Color to Replace the Old Color
Color newColor;

//Set the Color for newColor
newColor = Color.FromArgb(oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 - oldColor.B);

//Replace the Old Color with the New Color
bitmap.SetPixel(x, y, newColor);
}
}
//Return the Inverted Bitmap
return bitmap;
}

/// <summary>
/// Inverts an Image with the option to Invert it's Transparency.
/// </summary>
/// <param name="bitmap">The Bitmap Image you want to Invert.</param>
/// <param name="InvertAlpha">True to Invert it's Transparency. False to Invert the Image Only.</param>
/// <returns>An Inverted Image</returns>
public Bitmap Invert(Bitmap bitmap, bool InvertAlpha)
{
//X Axis
int x;
//Y Axis
int y;
//For the Width
for (x = 0; x <= bitmap.Width - 1; x++)
{
//For the Height
for (y = 0; y <= bitmap.Height - 1; y += 1)
{
//The Old Color to Replace
Color oldColor = bitmap.GetPixel(x, y);
//The New Color to Replace the Old Color
Color newColor;
//If the user has chosen to Invert the Transparency
if (InvertAlpha)
{
//Set the Color for newColor
newColor = Color.FromArgb(255 - oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 - oldColor.B);
}
else
{
//Set the Color for newColor
newColor = Color.FromArgb(oldColor.A, 255 - oldColor.R, 255 - oldColor.G, 255 - oldColor.B);
}
//Replace the Old Color with the New Color
bitmap.SetPixel(x, y, newColor);
}
}
//Return the Inverted Bitmap
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”