A color converter class

//http://calcsharp.codeplex.com/license
//Microsoft Public License (Ms-PL)
using System;
using System.Drawing;
using System.Text;
using System.Drawing.Imaging;

namespace CalcSharp.Core
{
///

/// A color converter class
///

public class Int24
{
private byte r;
private byte g;
private byte b;

private static void TestArrayLength(byte[] array, int l)
{
if (array.Length != l) throw new Exception(“Not enough data”);
}

public static int RGB2Int(byte R, byte G, byte B)
{
int temp = R << 16; int temp2 = G << 8; temp += temp2; temp2 = B; temp += temp2; return temp; } #region Properties public byte R { get { return this.r; } set { this.r = value; } } public byte G { get { return this.g; } set { this.g = value; } } public byte B { get { return this.b; } set { this.b = value; } } public static int MaxValue { get { return 0xFFFFFF; } } public static int MinValue { get { return 0; } } #endregion #region constructors public Int24() { this.r = 0; this.g = 0; this.b = 0; } public Int24(int num) { this.FromInt32(num); } public Int24(int R, int G, int B) { this.FromRGB(R, G, B); } public void FromRGB(int R, int G, int B) { if (R < 0) this.r = 0; else if (R > 255) this.r = 255;
else this.r = (byte)R;

if (G < 0) this.g = 0; else if (G > 255) this.g = 255;
else this.g = (byte)R;

if (B < 0) this.b = 0; else if (B > 255) this.b = 255;
else this.b = (byte)R;
}

public Int24(Color c)
{
this.FromColor(c);
}
#endregion

#region Operators & Overrides

public static implicit operator Int24(int input)
{
return new Int24(input);
}

public static implicit operator int(Int24 input)
{
return input.ToInt32();
}

public static Int24 operator +(Int24 i1, Int24 i2)
{
Int24 ret = new Int24();
int r, g, b;
r = i1.R + i2.R;
g = i1.G + i2.G;
b = i1.B + i2.B;
if (r > 255) r = 255;
if (r < 0) r = 0; if (g > 255) g = 255;
if (g < 0) g = 0; if (b > 255) b = 255;
if (b < 0) b = 0; ret.R = (byte)r; ret.G = (byte)g; ret.B = (byte)b; return ret; } public static Int24 operator +(Int24 i, int n) { Int24 ret = new Int24(); int r, g, b; r = i.R + n; g = i.G + n; b = i.B + n; if (r > 255) r = 255;
if (r < 0) r = 0; if (g > 255) g = 255;
if (g < 0) g = 0; if (b > 255) b = 255;
if (b < 0) b = 0; ret.R = (byte)r; ret.G = (byte)g; ret.B = (byte)b; return ret; } public static Int24 operator -(Int24 i1, Int24 i2) { Int24 ret = new Int24(); int r, g, b; r = i1.R - i2.R; g = i1.G - i2.G; b = i1.B - i2.B; if (r > 255) r = 255;
if (r < 0) r = 0; if (g > 255) g = 255;
if (g < 0) g = 0; if (b > 255) b = 255;
if (b < 0) b = 0; ret.R = (byte)r; ret.G = (byte)g; ret.B = (byte)b; return ret; } public static Int24 operator -(Int24 i, int n) { Int24 ret = new Int24(); int r, g, b; r = i.R - n; g = i.G - n; b = i.B - n; if (r > 255) r = 255;
if (r < 0) r = 0; if (g > 255) g = 255;
if (g < 0) g = 0; if (b > 255) b = 255;
if (b < 0) b = 0; ret.R = (byte)r; ret.G = (byte)g; ret.B = (byte)b; return ret; } public static Int24 operator *(Int24 i1, Int24 i2) { Int24 ret = new Int24(); int r, g, b; r = i1.R * i2.R; g = i1.G * i2.G; b = i1.B * i2.B; if (r > 255) r = 255;
if (r < 0) r = 0; if (g > 255) g = 255;
if (g < 0) g = 0; if (b > 255) b = 255;
if (b < 0) b = 0; ret.R = (byte)r; ret.G = (byte)g; ret.B = (byte)b; return ret; } public static Int24 operator *(Int24 i, int n) { Int24 ret = new Int24(); int r, g, b; r = i.R * n; g = i.G * n; b = i.B * n; if (r > 255) r = 255;
if (r < 0) r = 0; if (g > 255) g = 255;
if (g < 0) g = 0; if (b > 255) b = 255;
if (b < 0) b = 0; ret.R = (byte)r; ret.G = (byte)g; ret.B = (byte)b; return ret; } public static Int24 operator *(Int24 i, short n) { Int24 ret = new Int24(); int r, g, b; r = i.R * n; g = i.G * n; b = i.B * n; if (r > 255) r = 255;
if (r < 0) r = 0; if (g > 255) g = 255;
if (g < 0) g = 0; if (b > 255) b = 255;
if (b < 0) b = 0; ret.R = (byte)r; ret.G = (byte)g; ret.B = (byte)b; return ret; } public static Int24 operator /(Int24 i1, Int24 i2) { Int24 ret = new Int24(); int r, g, b; r = i1.R / i2.R; g = i1.G / i2.G; b = i1.B / i2.B; if (r > 255) r = 255;
if (r < 0) r = 0; if (g > 255) g = 255;
if (g < 0) g = 0; if (b > 255) b = 255;
if (b < 0) b = 0; ret.R = (byte)r; ret.G = (byte)g; ret.B = (byte)b; return ret; } public static Int24 operator /(Int24 i, int n) { Int24 ret = new Int24(); int r, g, b; r = i.R / n; g = i.G / n; b = i.B / n; if (r > 255) r = 255;
if (r < 0) r = 0; if (g > 255) g = 255;
if (g < 0) g = 0; if (b > 255) b = 255;
if (b < 0) b = 0; ret.R = (byte)r; ret.G = (byte)g; ret.B = (byte)b; return ret; } public static bool operator ==(Int24 a, Int24 b) { return (a.R == b.R && a.G == b.G && a.B == b.B); } public static bool operator !=(Int24 a, Int24 b) { return (a.R != b.R || a.G != b.G || a.B != b.B); } public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) return false; return (this == (Int24)obj); } public override int GetHashCode() { return R.GetHashCode() ^ G.GetHashCode() ^ B.GetHashCode(); } #endregion #region From public void FromInt32(int num) { int w = num; int temp; if (w < 0) w = Math.Abs(w); if (w > 0xFFFFFF) w = 0xFFFFFF;

temp = w & 0xFF0000;
temp = temp >> 16;
this.r = Convert.ToByte(temp);

temp = w & 0x00FF00;
temp = temp >> 8;
this.g = Convert.ToByte(temp);

temp = w & 0x0000FF;
this.b = Convert.ToByte(temp);
}

public void FromColor(Color c)
{
this.r = c.R;
this.b = c.B;
this.g = c.G;
}

public void FromByteArray(byte[] array, PixelFormat f)
{
ushort data;
switch (f)
{
case PixelFormat.Format16bppArgb1555:
case PixelFormat.Format16bppRgb555:
TestArrayLength(array, 2);
data = BitConverter.ToUInt16(array, 0);
this.r = (byte)((data & 0x7C00) >> 10);
this.g = (byte)((data & 0x3E0) >> 5);
this.b = (byte)((data & 0x1F));
break;
case PixelFormat.Format16bppRgb565:
TestArrayLength(array, 2);
data = BitConverter.ToUInt16(array, 0);
this.r = (byte)((data & 0xF800) >> 11);
this.g = (byte)((data & 0x7E0) >> 6);
this.b = (byte)(data & 0x1F);
break;
case PixelFormat.Format24bppRgb:
TestArrayLength(array, 3);
this.r = array[0];
this.g = array[1];
this.b = array[2];
break;
case PixelFormat.Format32bppArgb:
case PixelFormat.Format32bppRgb:
TestArrayLength(array, 4);
this.r = array[1];
this.g = array[2];
this.b = array[3];
break;
case PixelFormat.Format32bppPArgb:
TestArrayLength(array, 4);
byte alpha = array[0];
this.r = (byte)(array[1] / alpha);
this.g = (byte)(array[2] / alpha);
this.b = (byte)(array[3] / alpha);
break;
default:
throw new Exception(“Unsuported pixel format”);
}
}

public void FromByteArray(byte[] array)
{
byte backup;
Array.Reverse(array);
ushort b2, b;
switch (array.Length)
{
case 1:
backup = (byte)(array[0] & 192);
this.r = (byte)(backup >> 5);
backup = (byte)(array[0] & 28);
this.g = (byte)(backup >> 2);
this.b = (byte)(array[0] & 3);
break;
case 2:
//5650 format
b = BitConverter.ToUInt16(array, 0);
b2 = (ushort)(b & 0xf800);
this.r = (byte)(b2 >> 11);
b2 = (ushort)(b & 0x07e0);
this.g = (byte)(b2 >> 5);
this.b = (byte)(b & 0x001f);
break;
case 3:
this.r = array[0];
this.g = array[1];
this.b = array[2];
break;
case 4:
this.r = array[1];
this.g = array[2];
this.b = array[3];
break;
}
}

public void FromString(string val)
{
string[] raw = val.Split(';');
if (raw.Length != 3) throw new Exception(“Can't parse as HSL value: ” + val);
raw[0] = raw[0].Replace(“R: “, “”);
raw[1] = raw[1].Replace(” G: “, “”);
raw[2] = raw[2].Replace(” B: “, “”);
this.R = Convert.ToByte(raw[0]);
this.G = Convert.ToByte(raw[1]);
this.B = Convert.ToByte(raw[2]);
}
#endregion

#region To
public int ToInt32()
{
int temp = this.r << 16; int temp2 = this.g << 8; temp += temp2; temp2 = this.b; temp += temp2; return temp; } public byte[] ToByteArray() { byte[] ret = new byte[3]; ret[0] = this.r; ret[1] = this.g; ret[2] = this.b; return ret; } public Color ToColor() { return Color.FromArgb(this.r, this.g, this.b); } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append("R: " + r.ToString() + "; "); sb.Append("G: " + g.ToString() + "; "); sb.Append("B: " + b.ToString()); return sb.ToString(); } #endregion } } [/csharp]

Image manipulation using ColorMatrix

   
 

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pen Cap App";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Bitmap bmp = new Bitmap("winter.jpg");
      Graphics g = e.Graphics;

      float[][] matrixItems = {
                        new float[] {0.2f, 0, 0, 0, 0},
                        new float[] {0, 0.8f, 1, 0, 0},
                        new float[] {0, 0, 1, 0, 0},
                        new float[] {0, 0, 0, 1, 0}, 
                        new float[] {0, 0, 0, 0, 1}};
      ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

      ImageAttributes imageAtt = new ImageAttributes();
      imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

      TextureBrush tb = new TextureBrush(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height), imageAtt);
      tb.WrapMode = WrapMode.Tile;
      g.FillRectangle(tb, this.ClientRectangle);
      bmp.Dispose();
      tb.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
         
     


Use Color Matrix to gray the image


   
 

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  using System.Windows.Forms;
  using System.Data;
  using System.Drawing.Imaging;

  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

    }
    static void Main() 
    {
      Application.Run(new Form1());
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      Bitmap bmp = new Bitmap("winter.jpg");
      g.FillRectangle(Brushes.White, this.ClientRectangle);

      // Create a color matrix
      // The value 0.6 in row 4, column 4 specifies the alpha value
      float[][] matrixItems = {
                                new float[] {1, 0, 0, 0, 0},
                                new float[] {0, 1, 0, 0, 0},
                                new float[] {0, 0, 1, 0, 0},
                                new float[] {0, 0, 0, 0.6f, 0}, 
                                new float[] {0, 0, 0, 0, 1}};
      ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

      // Create an ImageAttributes object and set its color matrix
      ImageAttributes imageAtt = new ImageAttributes();
      imageAtt.SetColorMatrix(
        colorMatrix,
        ColorMatrixFlag.Default,
        ColorAdjustType.Bitmap);

      // Now draw the semitransparent bitmap image.
      g.DrawImage(bmp, this.ClientRectangle, 0.0f, 0.0f, bmp.Width, bmp.Height, 
                  GraphicsUnit.Pixel, imageAtt);

      imageAtt.Dispose();
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
         
     


Clipping


   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Drawing2D;

namespace Clipping
{
    /// <summary>
    /// Summary description for Clipping.
    /// </summary>
    public class Clipping : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;
        GraphicsPath gP;
        public Clipping()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            this.Text = "Transform and Clip";

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // Clipping
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 149);
            this.Name = "Clipping";
            this.Text = "Clipping";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Clipping());
        }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            DrawPathTransform(g);
            //DrawClip(g);
            g.Dispose();
        }
        void AddStringToPath(string s)
        {
            int fSt = (int)FontStyle.Regular;
            Point xy = new Point(50, 10);
            FontFamily fF = new FontFamily("Times new roman");
            StringFormat sFr = StringFormat.GenericDefault;

            gP.AddString(s, fF, fSt, 100, xy, sFr);  // add the string to the path
        }
        void DrawPathTransform(Graphics g)
        {
            gP = new GraphicsPath();  // create a new path
            AddStringToPath("C#");

            g.FillPath(Brushes.Gray, gP);  // draw the path to the surface
            Matrix m = new Matrix();
            m.Translate(5, 5);
            gP.Transform(m);
            g.FillPath(Brushes.Yellow, gP);  // draw the path to the surface
            g.DrawPath(Pens.Red, gP);  // draw the path to the surface
        }
        void DrawClip(Graphics g)
        {
            gP = new GraphicsPath();  // create a new path
            AddStringToPath("C#");

            g.SetClip(gP);
            g.TranslateClip(-5, -5);

            RectangleF rc = gP.GetBounds();
            rc.Offset(-5, -5);
            g.FillRectangle(Brushes.Gray, rc);  // results in gray C#

            g.ResetClip();
            g.FillPath(Brushes.Yellow, gP);  // draw the path to the surface
            g.DrawPath(Pens.Red, gP);  // draw the path to the surface
        }
    }
}

           
          


Clip Draw


   

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ClipDraw_c
{
    /// <summary>
    /// Summary description for ClipDraw_c.
    /// </summary>
    public class ClipDraw_c : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button B1;
        private System.Windows.Forms.Button B2;
        private System.Windows.Forms.Button B3;
        
        internal System.Windows.Forms.Button B4;
        internal System.Windows.Forms.Button B5;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ClipDraw_c() {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
            this.MouseMove += new MouseEventHandler(this.ClipDraw_cMouse);

        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.B1 = new System.Windows.Forms.Button();
            this.B2 = new System.Windows.Forms.Button();
            this.B3 = new System.Windows.Forms.Button();
            this.B4 = new System.Windows.Forms.Button();
            this.B5 = new System.Windows.Forms.Button();
            this.SuspendLayout();
              // 
            // B1
            // 
            this.B1.Location = new System.Drawing.Point(16, 232);
            this.B1.Name = "B1";
            this.B1.Size = new System.Drawing.Size(56, 24);
            this.B1.TabIndex = 0;
            this.B1.Text = "Clip";
            this.B1.Click += new System.EventHandler(this.B1_Click);
            // 
            // B2
            // 
            this.B2.Location = new System.Drawing.Point(96, 232);
            this.B2.Name = "B2";
            this.B2.Size = new System.Drawing.Size(56, 24);
            this.B2.TabIndex = 1;
            this.B2.Text = "SetClip";
            this.B2.Click += new System.EventHandler(this.B2_Click);
            // 
            // B3
            // 
            this.B3.Location = new System.Drawing.Point(176, 232);
            this.B3.Name = "B3";
            this.B3.Size = new System.Drawing.Size(88, 24);
            this.B3.TabIndex = 2;
            this.B3.Text = "ExcludeClip";
            this.B3.Click += new System.EventHandler(this.B3_Click);
            // 
            // B4
            // 
            this.B4.Location = new System.Drawing.Point(176, 200);
            this.B4.Name = "B4";
            this.B4.Size = new System.Drawing.Size(88, 24);
            this.B4.TabIndex = 4;
            this.B4.Text = "Intersect";
            this.B4.Click += new System.EventHandler(this.B4_Click);
            // 
            // B5
            // 
            this.B5.Location = new System.Drawing.Point(280, 232);
            this.B5.Name = "B5";
            this.B5.Size = new System.Drawing.Size(88, 24);
            this.B5.TabIndex = 5;
            this.B5.Text = "IntersectClip";
            this.B5.Click += new System.EventHandler(this.B5_Click);
            // 
            // ClipDraw_c
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(376, 273);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.B5,
                                                                  this.B4,
                                                                  this.B3,
                                                                  this.B2,
                                                                  this.B1});
            this.Name = "ClipDraw_c";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "ClipDraw_c";
            this.Load += new System.EventHandler(this.ClipDraw_c_Load);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new ClipDraw_c());
        }

        private Graphics G_obj;

        private void ClipDraw_c_Load(object sender, System.EventArgs e)
        {
    
        }
        private void ClipDraw_cMouse(System.Object sender , MouseEventArgs e) 
        {
            if (G_obj == null)
                return;
            if (G_obj.IsVisible(e.X, e.Y))
                G_obj.FillRectangle(Brushes.BlueViolet, e.X, e.Y, 1, 1);
        }

        private void B1_Click(object sender, System.EventArgs e)
        {
            Graphics G  = this.CreateGraphics();
            Rectangle R = new Rectangle(50, 50, 100, 100);

            G.Clear(Color.Gainsboro);

            //Outline the rectangle and create the clipping region
            G.DrawRectangle(Pens.Black, R);
            G.Clip = new Region(R);

            //Draw line the sidth of the form
            G.DrawLine(Pens.Blue, 0, 75, this.Width, 75);
            //Draw a circle 1/4 inside clipping region
            G.FillEllipse(Brushes.LawnGreen, new Rectangle(75, 75, 150, 150));

            G.Dispose();    
        }

        private void B2_Click(object sender, System.EventArgs e)
        {
            Graphics G = this.CreateGraphics();
            Rectangle R = new Rectangle(20, 20, 100, 100);
            GraphicsPath path = new GraphicsPath();

            G.Clear(Color.Gainsboro);
            path.AddEllipse(R);
            G.DrawPath(Pens.Black, path);
            G.SetClip(path);

            // Draw some clipped strings.
            Font F = new Font("Arial", 16);
            G.DrawString("ABCDEFGHIJKLM", F, Brushes.DeepPink, 15, 25);
            G.DrawString("NOPQRSTUVWXYZ", F, Brushes.DeepPink, 15, 68);

            path.Dispose();

        }

        private void B3_Click(object sender, System.EventArgs e)
        {
            Graphics G = this.CreateGraphics();
            Rectangle R = new Rectangle(40, 20, 100, 100);
            GraphicsPath path = new GraphicsPath();

            G.Clear(Color.Gainsboro);
            path.AddEllipse(R);
            G.DrawPath(Pens.Black, path);
            G.ExcludeClip(new Region(path));

            // Draw some clipped strings.
            Font F = new Font("Arial", 16);
            G.DrawString("ABCDEFGHIJKLM", F, Brushes.DeepPink, 15, 25);
            G.DrawString("NOPQRSTUVWXYZ", F, Brushes.DeepPink, 15, 68);

            path.Dispose();    
            F.Dispose();
        }

        private void B4_Click(object sender, System.EventArgs e)
        {
      Graphics G = this.CreateGraphics();
      Rectangle R = new Rectangle(40, 20, 100, 100);
      Rectangle R2 = new Rectangle(60, 20, 120, 120);
      GraphicsPath P1 = new GraphicsPath();
      GraphicsPath P2 = new GraphicsPath();

      G.Clear(Color.Gainsboro);
      P1.AddEllipse(R);
      G.DrawPath(Pens.Black, P1);

      P2.AddEllipse(R2);
      G.DrawPath(Pens.Black, P2);

      G.SetClip(P1);
      G.SetClip(P2, CombineMode.Intersect);

      // Draw some clipped strings.
      Font F = new Font("Arial", 16);
      G.DrawString("ABCDEFGHIJKLM", F, Brushes.DeepPink, 15, 25);
      G.DrawString("NOPQRSTUVWXYZ", F, Brushes.DeepPink, 15, 68);

      G_obj = G;
      F.Dispose();
      P1.Dispose();     
      P2.Dispose();     
    }

    private void B5_Click(object sender, System.EventArgs e)
    {
      //Bail out if not set
      if (G_obj == null )
        return;
      Rectangle R2 = new Rectangle(75, 75, 150, 150);

      G_obj.IntersectClip(R2);
      G_obj.FillRectangle(new SolidBrush(Color.Blue), 0, 0, 500, 500);
    }
    }
}


           
          


Clipping Combinations

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class ClippingCombinations: Form
{
string strCaption = “CombineMode = “;
MenuItem miCombineMode;

public static void Main()
{
Application.Run(new ClippingCombinations());
}
protected override void OnPaint(PaintEventArgs pea)
{
DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
}
public ClippingCombinations()
{
ResizeRedraw = true;

Menu = new MainMenu();
Menu.MenuItems.Add(“&CombineMode”);

EventHandler ehClick = new EventHandler(MenuCombineModeOnClick);

for (int i = 0; i < 6; i++) { MenuItem mi = new MenuItem("&" + (CombineMode)i); mi.Click += ehClick; mi.RadioCheck = true; Menu.MenuItems[0].MenuItems.Add(mi); } miCombineMode = Menu.MenuItems[0].MenuItems[0]; miCombineMode.Checked = true; } void MenuCombineModeOnClick(object obj, EventArgs ea) { miCombineMode.Checked = false; miCombineMode = (MenuItem) obj; miCombineMode.Checked = true; Text = strCaption + (CombineMode)miCombineMode.Index; Invalidate(); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { GraphicsPath path = new GraphicsPath(); path.AddEllipse(0, 0, 2 * cx / 3, cy); grfx.SetClip(path); path.Reset(); path.AddEllipse(cx / 3, 0, 2 * cx / 3, cy); grfx.SetClip(path, (CombineMode)miCombineMode.Index); grfx.FillRectangle(Brushes.Red, 0, 0, cx, cy); } } [/csharp]

Write info about the clipping rectangle to the console window

   
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    String s = "text";
    Font f = new Font("Times New Roman", 12);
    SizeF sf = g.MeasureString(s, f, 400);
    RectangleF rf = new RectangleF(10, 10, sf.Width, sf.Height);

    g.DrawString(s, f, Brushes.Black, rf);
    f.Dispose();
    Console.WriteLine("Clipping region painted: " + e.ClipRectangle);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}