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 개체는 DrawLineDrawRectangle과 같은 그리기 메서드를 더 많이 제공합니다. 또한 Graphics 개체는 그래픽 상태를 유지 관리하는데, 이것은 다음 범주로 나뉠 수 있습니다.

  • 품질 설정

  • 변환

  • 클리핑 영역

품질 설정

Graphics 개체는 그려진 항목의 품질에 영향을 주는 여러 가지 속성을 가집니다. 예를 들어, TextRenderingHint 속성을 설정하여 텍스트에 적용되는 앤티앨리어싱(있는 경우) 유형을 지정할 수 있습니다. 품질에 영향을 주는 기타 속성은 SmoothingMode, CompositingMode, CompositingQualityInterpolationMode입니다.

다음 예는 두 개의 타원을 그리는데, 하나는 다듬기 모드를 AntiAlias로 설정한 것이고, 다른 하나는 다듬기 모드를 HighSpeed로 설정한 것입니다.

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);  

다음 그림은 클리핑된 선을 보여줍니다.

제한된 클립 영역을 보여주는 다이어그램.

참고 항목