다음을 통해 공유


Graphics 개체의 상태 관리

Graphics 클래스는 GDI+의 핵심입니다. 원하는 요소를 그리려면 Graphics 개체를 가져와 속성을 설정하고 DrawLine, DrawImage, DrawString 등과 같은 메서드를 호출합니다.

다음 예제에서는 Graphics 개체의 DrawRectangle 메서드를 호출합니다. DrawRectangle 메서드에 전달되는 첫 번째 인수는 Pen 개체입니다.

Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Blue) ' Opaque blue
graphics.DrawRectangle(pen, 10, 10, 200, 100)
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Blue);  // Opaque blue
graphics.DrawRectangle(pen, 10, 10, 200, 100);

Graphics 상태

Graphics 개체는 DrawLineDrawRectangle 같은 그리기 메서드를 제공합니다. 그 뿐만 아니라 Graphics 개체는 다음과 같은 범주로 구분되는 그래픽 상태를 유지 관리합니다.

  • 품질 설정

  • 변환

  • 클리핑 영역

품질 설정

Graphics 개체에는 그리는 항목의 품질에 영향을 주는 몇 가지 속성이 있습니다. 예를 들면 TextRenderingHint 속성을 설정하여 텍스트에 적용되는 앤티 앨리어싱(있을 경우)의 종류를 지정할 수 있습니다. 이 외에도 SmoothingMode, CompositingMode, CompositingQualityInterpolationMode 같은 속성이 품질에 영향을 줍니다.

다음 예제에서는 다듬기 모드 속성을 각각 AntiAliasHighSpeed로 설정한 타원 두 개를 그립니다.

Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Blue)
      
graphics.SmoothingMode = SmoothingMode.AntiAlias
graphics.DrawEllipse(pen, 0, 0, 200, 100)
graphics.SmoothingMode = SmoothingMode.HighSpeed
graphics.DrawEllipse(pen, 0, 150, 200, 100)
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Blue);

graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawEllipse(pen, 0, 0, 200, 100);
graphics.SmoothingMode = SmoothingMode.HighSpeed;
graphics.DrawEllipse(pen, 0, 150, 200, 100);

변환

Graphics 개체는 해당 Graphics 개체에서 그리는 모든 항목에 적용되는 전역 변환과 페이지 변환을 유지 관리합니다. 상관 변환은 전역 변환에 저장할 수 있습니다. 상관 변환으로는 배율 조정, 회전, 반사, 기울이기, 이동 등이 있습니다. 페이지 변환은 픽셀을 인치로 바꾸는 등의 단위 변환이나 배율 조정을 위해 사용할 수 있습니다. 자세한 내용은 좌표계 및 변환을 참조하십시오.

다음 예제에서는 Graphics 개체의 전역 변환과 페이지 변환을 설정합니다. 전역 변환은 30도 회전으로 설정되어 있습니다. 페이지 변환은 두 번째 DrawEllipse에 전달되는 좌표를 픽셀 대신 밀리미터로 처리하도록 설정되어 있습니다. 이 코드는 DrawEllipse 메서드를 동일한 방법으로 두 번 호출합니다. 첫 번째 DrawEllipse 호출에는 전역 변환이 적용되고 두 번째 DrawEllipse 호출에는 전역 변환과 페이지 변환이 모두 적용됩니다.

Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Red)
      
graphics.ResetTransform()
graphics.RotateTransform(30) ' world transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50)
graphics.PageUnit = GraphicsUnit.Millimeter ' page transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50)
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Red); 

graphics.ResetTransform();
graphics.RotateTransform(30);                    // world transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50);
graphics.PageUnit = GraphicsUnit.Millimeter;     // page transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50);

아래 그림에 두 개의 타원이 나와 있습니다. 여기서 30도 회전은 타원의 중앙이 아니라 좌표계의 원점(클라이언트 영역의 왼쪽 위 모퉁이)을 기준으로 합니다. 또한, 펜 굵기는 똑같이 1이지만 첫 번째 타원은 1픽셀 굵기로 그려지고 두 번째 타원은 1밀리미터 굵기로 그려집니다.

타원

클리핑 영역

Graphics 개체는 해당 Graphics 개체에서 그리는 모든 항목에 적용되는 클리핑 영역을 유지 관리합니다. SetClip 메서드를 호출하여 클리핑 영역을 설정할 수 있습니다.

아래 예제에서는 두 개의 사각형을 결합하여 더하기 기호 모양의 영역을 만듭니다. 이 영역은 Graphics 개체의 클리핑 영역으로 지정됩니다. 그런 다음 코드에서는 클리핑 영역 내부로 제한되는 두 개의 선을 그립니다.

Dim graphics As Graphics = e.Graphics
      
' Opaque red, width 5
Dim pen As New Pen(Color.Red, 5)
      
' Opaque aqua
Dim brush As New SolidBrush(Color.FromArgb(255, 180, 255, 255))
      
' Create a plus-shaped region by forming the union of two rectangles.
Dim [region] As New [Region](New Rectangle(50, 0, 50, 150))
[region].Union(New Rectangle(0, 50, 150, 50))
graphics.FillRegion(brush, [region])
      
' Set the clipping region.
graphics.SetClip([region], CombineMode.Replace)
      
' Draw two clipped lines.
graphics.DrawLine(pen, 0, 30, 150, 160)
graphics.DrawLine(pen, 40, 20, 190, 150)
Graphics graphics = e.Graphics;

// Opaque red, width 5
Pen pen = new Pen(Color.Red, 5);  

// Opaque aqua
SolidBrush brush = new SolidBrush(Color.FromArgb(255, 180, 255, 255));  

// Create a plus-shaped region by forming the union of two rectangles.
Region region = new Region(new Rectangle(50, 0, 50, 150));
region.Union(new Rectangle(0, 50, 150, 50));
graphics.FillRegion(brush, region);

// Set the clipping region.
graphics.SetClip(region, CombineMode.Replace);

// Draw two clipped lines.
graphics.DrawLine(pen, 0, 30, 150, 160);
graphics.DrawLine(pen, 40, 20, 190, 150);

아래 그림에 클리핑된 선이 나와 있습니다.

제한된 클립 영역

참고 항목

개념

중첩된 Graphics 컨테이너 사용

기타 리소스

Windows Forms의 그래픽 및 그리기