System.Drawing.Bitmap.SetPixel C# (CSharp) Method

SetPixel() public method

public SetPixel ( int x, int y, Color color ) : void
x int
y int
color Color
return void
        public void SetPixel(int x, int y, Color color)
        {
            if (x < 0 || x > NativeCGImage.Width - 1)
                throw new InvalidEnumArgumentException ("Parameter must be positive and < Width.");
            if (y < 0 || y > NativeCGImage.Height - 1)
                throw new InvalidEnumArgumentException ("Parameter must be positive and < Height.");

            MakeSureWeHaveAnAlphaChannel ();

            // We are going to cheat here by drawing directly to the cached context that is
            // associated to the image.  This way we do not have to play with pixels and offsets
            // to change the data.  If this proves to be non performant then we will change it later.
            cachedContext.SaveState ();
            cachedContext.ConcatCTM (cachedContext.GetCTM ().Invert ());
            cachedContext.ConcatCTM (imageTransform);
            cachedContext.SetFillColor(color.ToCGColor());
            cachedContext.FillRect (new CGRect (x,y,1,1));
            cachedContext.FillPath ();
            cachedContext.RestoreState();
        }

Usage Example

Example #1
2
        public static void Remove(int colour, ref Bitmap bp)
        {
            Color c;
              switch (colour)
              {
            case BLUE:
              for (int i = 1; i < bp.Width; i++)
            for (int j = 1; j < bp.Height; j++)
            {
              c = bp.GetPixel(i, j);
              bp.SetPixel(i, j, Color.FromArgb(c.R, c.G, 0));
            }
              break;

            case RED:
              for (int i = 1; i < bp.Width; i++)
            for (int j = 1; j < bp.Height; j++)
            {
              c = bp.GetPixel(i, j);
              bp.SetPixel(i, j, Color.FromArgb(0, c.G, c.B));
            }
              break;

            case GREEN:
              for (int i = 1; i < bp.Width; i++)
            for (int j = 1; j < bp.Height; j++)
            {
              c = bp.GetPixel(i, j);
              bp.SetPixel(i, j, Color.FromArgb(c.R, 0, c.B));
            }
              break;
              }
        }
All Usage Examples Of System.Drawing.Bitmap::SetPixel