Clip many shapes : Region Clip « 2D Graphics « C# / C Sharp






Clip many shapes

Clip many shapes

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

public class Form1 : Form
{
    private System.Windows.Forms.CheckBox chkClipping;
  public Form1() {
        InitializeComponent();
  }

  private void checkBox1_CheckedChanged(object sender, EventArgs e)
  {
    Invalidate();
  }  
  private void Form1_Paint(object sender, PaintEventArgs e)
  {
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    GraphicsPath path = new GraphicsPath();
    
    Region clippingRegion = new Region(path);
    if (chkClipping.Checked) e.Graphics.Clip = clippingRegion;

    e.Graphics.ResetClip();
    
    path = new GraphicsPath();
    path.AddString("www.java2s.com", new FontFamily("Verdana"), 0, 70, new Point(10, 130), new StringFormat());
    e.Graphics.DrawPath(Pens.Blue, path);

    clippingRegion = new Region(path);
    if (chkClipping.Checked) e.Graphics.Clip = clippingRegion;

    for (int i = 0; i < 40; i++)
    {
      e.Graphics.DrawEllipse(Pens.Red, 180 - i*3, 180 - i*3, i*6, i*6);      
    }
    clippingRegion.Dispose();
    path.Dispose();

  }

  private void InitializeComponent()
  {
    this.chkClipping = new System.Windows.Forms.CheckBox();
    this.SuspendLayout();
    // 
    // chkClipping
    // 
    this.chkClipping.AutoSize = true;
    this.chkClipping.Checked = true;
    this.chkClipping.CheckState = System.Windows.Forms.CheckState.Checked;
    this.chkClipping.Location = new System.Drawing.Point(12, 300);
    this.chkClipping.Name = "chkClipping";
    this.chkClipping.Size = new System.Drawing.Size(80, 17);
    this.chkClipping.TabIndex = 0;
    this.chkClipping.Text = "Use Clipping";
    this.chkClipping.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(292, 329);
    this.Controls.Add(this.chkClipping);
    this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.Name = "Form1";
    this.Text = "Clipping";
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    this.ResumeLayout(false);
    this.PerformLayout();

  }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}


           
       








Related examples in the same category

1.Text region clips shapeText region clips shape