How do I create a ‘Negative’ representation of my image?

Platform: WinForms| Category: Bitmaps and Images

This is quite simple actually. All you need to do is determine the color of a given pixel, and then subtract the RGB values from 255 (the maximum) and then reset the pixel.

Here is the pertinent code.

[Code - C#]

for(int x = 0; x < mybitmap.Width; x++)
{
	for(int y = 0; y < mybitmap.Height; y++)
	{
		//get the current color of the pixel
		System.Drawing.Color c = mybitmap.GetPixel(x,y);
				
		mybitmap.SetPixel(x,y, System.Drawing.Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B));
	}
}

Note: This modifies the existing image, so work on a copy if you want to maintain the original.

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.