Inverts the colors in a bitmap. - CSharp System.Drawing

CSharp examples for System.Drawing:Bitmap

Description

Inverts the colors in a bitmap.

Demo Code


using System.Windows.Forms;
using System.Text;
using System.Linq;
using System.Drawing;
using System.Collections.Generic;
using System;//from   w  w  w . j av  a2 s. c  o m

public class Main{
    /// <summary>
      /// Inverts the colors in a bitmap.
      /// </summary>
      public static Bitmap Invert(Bitmap source) {
         Bitmap ret = new Bitmap(source.Width, source.Height);
         for (int x = 0; x < ret.Width; x++) {
            for (int y = 0; y < ret.Height; y++) {
               Color c = source.GetPixel(x, y);
               ret.SetPixel(x, y, Color.FromArgb(c.A, 255-c.R, 255-c.G, 255-c.B));
            }
         }
         return ret;
      }
}

Related Tutorials