Mega Code Archive

 
Categories / C# / 2D Graphics
 

Clipping

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D; public class Form1 : System.Windows.Forms.Form {     GraphicsPath gP;     [STAThread]     static void Main() {         Application.Run(new Form1());     }     protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {         Graphics g = e.Graphics;         DrawPathTransform(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);     }     void DrawPathTransform(Graphics g) {         gP = new GraphicsPath();         AddStringToPath("C#");         g.FillPath(Brushes.Gray, gP);         Matrix m = new Matrix();         m.Translate(5, 5);         gP.Transform(m);         g.FillPath(Brushes.Yellow, gP);         g.DrawPath(Pens.Red, gP);     }     void DrawClip(Graphics g) {         gP = new GraphicsPath();         AddStringToPath("C#");         g.SetClip(gP);         g.TranslateClip(-5, -5);         RectangleF rc = gP.GetBounds();         rc.Offset(-5, -5);         g.FillRectangle(Brushes.Gray, rc);         g.ResetClip();         g.FillPath(Brushes.Yellow, gP);         g.DrawPath(Pens.Red, gP);     } }