다음을 통해 공유


방법: 영역에 클리핑 사용

Graphics 클래스의 속성 중 하나는 클립 영역입니다. 지정된 Graphics 개체에 의해 수행된 모든 그리기는 해당 Graphics 개체의 클립 영역으로 제한됩니다. SetClip 메서드를 호출하여 클립 영역을 설정할 수 있습니다.

예제

다음 예제에서는 단일 다각형으로 구성된 경로를 생성합니다. 그런 다음, 코드는 해당 경로에 따라 지역을 생성합니다. 영역은 Graphics 개체의 SetClip 메서드에 전달된 다음, 두 개의 문자열이 그려집니다.

다음 그림은 클리핑된 문자열을 보여 줍니다.

클리핑된 문자열을 보여주는 스크린샷.

     // Create a path that consists of a single polygon.
     Point[] polyPoints = {
new Point(10, 10),
new Point(150, 10),
new Point(100, 75),
new Point(100, 150)};
     GraphicsPath path = new GraphicsPath();
     path.AddPolygon(polyPoints);

     // Construct a region based on the path.
     Region region = new Region(path);

     // Draw the outline of the region.
     Pen pen = Pens.Black;
     e.Graphics.DrawPath(pen, path);

     // Set the clipping region of the Graphics object.
     e.Graphics.SetClip(region, CombineMode.Replace);

     // Draw some clipped strings.
     FontFamily fontFamily = new FontFamily("Arial");
     Font font = new Font(
        fontFamily,
        36, FontStyle.Bold,
        GraphicsUnit.Pixel);
     SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 255, 0, 0));

     e.Graphics.DrawString(
        "A Clipping Region",
        font, solidBrush,
        new PointF(15, 25));

     e.Graphics.DrawString(
        "A Clipping Region",
        font,
        solidBrush,
        new PointF(15, 68));
' Create a path that consists of a single polygon.
Dim polyPoints As Point() = { _
   New Point(10, 10), _
   New Point(150, 10), _
   New Point(100, 75), _
   New Point(100, 150)}
Dim path As New GraphicsPath()
path.AddPolygon(polyPoints)

' Construct a region based on the path.
Dim [region] As New [Region](path)

' Draw the outline of the region.
Dim pen As Pen = Pens.Black
e.Graphics.DrawPath(pen, path)

' Set the clipping region of the Graphics object.
e.Graphics.SetClip([region], CombineMode.Replace)

' Draw some clipped strings.
Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
   fontFamily, _
   36, _
   FontStyle.Bold, _
   GraphicsUnit.Pixel)
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 255, 0, 0))

e.Graphics.DrawString( _
   "A Clipping Region", _
   font, _
   solidBrush, _
   New PointF(15, 25))

e.Graphics.DrawString( _
   "A Clipping Region", _
   font, _
   solidBrush, _
   New PointF(15, 68))

코드 컴파일

앞의 예제는 Windows Forms에서 사용하도록 설계되었으며 PaintEventHandler의 매개 변수인 PaintEventArgse가 필요합니다.

참고 항목