Restricting the Drawing Surface in GDI+

Clipping involves restricting drawing to a certain rectangle or region. The following illustration shows the string "Hello" clipped to a heart-shaped region.

Screenshot of a heart-shaped region with the text string Hello inside the heart.

Clipping with Regions

Regions can be constructed from paths, and paths can contain the outlines of strings, so you can use outlined text for clipping. The following illustration shows a set of concentric ellipses clipped to the interior of a string of text.

Screenshot of the text string Hello with a set of concentric ellipses clipped to the interior of the text.

To draw with clipping, create a Graphics object, set its Clip property, and then call the drawing methods of that same Graphics object:

myGraphics.Clip = myRegion;
myGraphics.DrawLine(myPen, 0, 0, 200, 200);
myGraphics.Clip = myRegion
myGraphics.DrawLine(myPen, 0, 0, 200, 200)

See also