// Create a Graphics object Graphics graphics = this.CreateGraphics(); // Set the clipping region to a rectangle Rectangle clipRect = new Rectangle(50, 50, 100, 100); graphics.SetClip(clipRect); // Draw a rectangle within the clipping region graphics.DrawRectangle(Pens.Black, 0, 0, 200, 200); // Dispose of the Graphics object graphics.Dispose();
// Create a Graphics object Graphics graphics = this.CreateGraphics(); // Create a GraphicsPath object GraphicsPath clipPath = new GraphicsPath(); // Add a bezier curve to the path Point[] points = { new Point(10, 10), new Point(100, 150), new Point(200, 50), new Point(250, 250) }; clipPath.AddBeziers(points); // Set the clipping region to the path graphics.SetClip(clipPath); // Draw a circle within the clipping region graphics.DrawEllipse(Pens.Black, 0, 0, 300, 300); // Dispose of the Graphics object graphics.Dispose();In this example, we create a Graphics object and a GraphicsPath object. We add a bezier curve to the path using the AddBeziers method. We then set the clipping region to the path using the SetClip method. Finally, we draw a circle within the clipping region using the DrawEllipse method. We dispose of the Graphics object at the end. The System.Drawing namespace provides the Graphics class and associated types for drawing on a surface.