다음을 통해 공유


전역 및 지역 변환

전역 변환은 지정된 Graphics 개체가 그린 모든 항목에 적용되는 변환입니다. 반면 로컬 변환은 그릴 특정 항목에 적용되는 변환입니다.

전역 변환

전역 변환을 만들려면 Graphics 개체를 생성한 다음, 해당 Transform 속성을 조작합니다. Transform 속성은 Matrix 개체이므로 모든 아핀 변환 시퀀스를 보유할 수 있습니다. Transform 속성에 저장된 변환을 월드 변환이라고 합니다. Graphics 클래스는 복합 월드 변환을 빌드하기 위해 MultiplyTransform, RotateTransform, ScaleTransform, TranslateTransform과 같은 여러 가지 메서드를 제공합니다. 다음 예제에서는 타원을 두 번 그립니다. 한 번은 월드 변환을 만들기 전에 한 번, 그 후에 한 번은 그립니다. 변환은 먼저 y 방향으로 0.5의 배율을 조정한 다음 x 방향으로 50 단위를 변환한 다음 30도 회전합니다.

myGraphics.DrawEllipse(myPen, 0, 0, 100, 50);
myGraphics.ScaleTransform(1, 0.5f);
myGraphics.TranslateTransform(50, 0, MatrixOrder.Append);
myGraphics.RotateTransform(30, MatrixOrder.Append);
myGraphics.DrawEllipse(myPen, 0, 0, 100, 50);
myGraphics.DrawEllipse(myPen, 0, 0, 100, 50)
myGraphics.ScaleTransform(1, 0.5F)
myGraphics.TranslateTransform(50, 0, MatrixOrder.Append)
myGraphics.RotateTransform(30, MatrixOrder.Append)
myGraphics.DrawEllipse(myPen, 0, 0, 100, 50)

다음 그림에서는 변환과 관련된 행렬을 보여 줍니다.

Illustration of the Scale, Translate, and Rotate matrices combining to form the global transformation.전역 변환을 형성하기 위해 결합된 행렬의 배율, 좌표 이동 및 회전 그림입니다.AboutGdip05_art14

비고

앞의 예제에서 타원은 클라이언트 영역의 왼쪽 위 모서리에 있는 좌표계의 원점에 대해 회전됩니다. 이렇게 하면 타원을 자체 가운데에 대해 회전하는 것과는 다른 결과가 생성됩니다.

로컬 변환

로컬 변환은 그릴 특정 항목에 적용됩니다. 예를 들어 GraphicsPath 개체에는 해당 경로의 데이터 요소를 변환할 수 있는 Transform 메서드가 있습니다. 다음은 변환이 없는 사각형과 회전 변환이 있는 경로를 그리는 예제입니다. (세계 변환이 없다고 가정합니다.)

Matrix myMatrix = new Matrix();
myMatrix.Rotate(45);
myGraphicsPath.Transform(myMatrix);
myGraphics.DrawRectangle(myPen, 10, 10, 100, 50);
myGraphics.DrawPath(myPen, myGraphicsPath);
Dim myMatrix As New Matrix()
myMatrix.Rotate(45)
myGraphicsPath.Transform(myMatrix)
myGraphics.DrawRectangle(myPen, 10, 10, 100, 50)
myGraphics.DrawPath(myPen, myGraphicsPath)

세계 변환을 로컬 변환과 결합하여 다양한 결과를 얻을 수 있습니다. 예를 들어 월드 변환을 사용하여 좌표계를 수정하고 로컬 변환을 사용하여 새 좌표계에 그려진 개체를 회전하고 크기를 조정할 수 있습니다.

클라이언트 영역의 왼쪽 가장자리에서 원점이 200픽셀이고 클라이언트 영역 위쪽에서 150픽셀이 있는 좌표계를 사용한다고 가정합니다. 또한 x축이 오른쪽을 가리키고 y축이 위로 가리키도록 측정 단위를 픽셀로 지정한다고 가정합니다. 기본 좌표계에는 아래쪽을 가리키는 y축이 있으므로 가로 축을 통해 리플렉션을 수행해야 합니다. 다음 그림에서는 이러한 리플렉션의 행렬을 보여 줍니다.

Illustration of a matrix that reflects across the horizontal axis.가로 축을 가로질러 반사되는 행렬의 그림입니다.AboutGdip05_art15

다음으로, 오른쪽으로 200단위, 아래로 150단위 변환을 수행해야 한다고 가정합니다.

다음 예제에서는 Graphics 개체의 월드 변환을 설정하여 방금 설명한 좌표계를 설정합니다.

Matrix myMatrix = new Matrix(1, 0, 0, -1, 0, 0);
myGraphics.Transform = myMatrix;
myGraphics.TranslateTransform(200, 150, MatrixOrder.Append);
Dim myMatrix As New Matrix(1, 0, 0, -1, 0, 0)
myGraphics.Transform = myMatrix
myGraphics.TranslateTransform(200, 150, MatrixOrder.Append)

앞의 예제 끝에 배치된 다음 코드는 새 좌표계의 원점에서 왼쪽 아래 모서리가 있는 단일 사각형으로 구성된 경로를 만듭니다. 사각형은 로컬 변환 없이 한 번 채워지고 한 번은 로컬 변환으로 채워집니다. 로컬 변환은 가로 배율 2와 30도 회전으로 구성됩니다.

// Create the path.
GraphicsPath myGraphicsPath = new GraphicsPath();
Rectangle myRectangle = new Rectangle(0, 0, 60, 60);
myGraphicsPath.AddRectangle(myRectangle);

// Fill the path on the new coordinate system.
// No local transformation
myGraphics.FillPath(mySolidBrush1, myGraphicsPath);

// Set the local transformation of the GraphicsPath object.
Matrix myPathMatrix = new Matrix();
myPathMatrix.Scale(2, 1);
myPathMatrix.Rotate(30, MatrixOrder.Append);
myGraphicsPath.Transform(myPathMatrix);

// Fill the transformed path on the new coordinate system.
myGraphics.FillPath(mySolidBrush2, myGraphicsPath);
' Create the path.
Dim myGraphicsPath As New GraphicsPath()
Dim myRectangle As New Rectangle(0, 0, 60, 60)
myGraphicsPath.AddRectangle(myRectangle)

' Fill the path on the new coordinate system.
' No local transformation
myGraphics.FillPath(mySolidBrush1, myGraphicsPath)

' Set the local transformation of the GraphicsPath object.
Dim myPathMatrix As New Matrix()
myPathMatrix.Scale(2, 1)
myPathMatrix.Rotate(30, MatrixOrder.Append)
myGraphicsPath.Transform(myPathMatrix)

' Fill the transformed path on the new coordinate system.
myGraphics.FillPath(mySolidBrush2, myGraphicsPath)

다음 그림에서는 새 좌표계와 두 사각형을 보여 줍니다.

Illustration of the new coordinate system and the two rectangles.새 좌표계와 두 사각형을 보여주는 그림.AboutGdip05_art16

참고하십시오