全域和區域轉換

全域轉換是套用至指定 Graphics 物件所繪製之每個專案的轉換。 相反地,本機轉換是套用至要繪製之特定專案的轉換。

全域轉換

若要建立全域轉換,請建構 Graphics 物件,然後操作其 Transform 屬性。 屬性 TransformMatrix 物件,因此它可以保存任何序列的仿射轉換。 儲存在 屬性中的 Transform 轉換稱為世界轉換。 類別 Graphics 提供數種方法來建立複合世界轉換: MultiplyTransformRotateTransformScaleTransformTranslateTransform 。 下列範例會繪製兩次橢圓形:建立世界轉換之前,一次之後再繪製一次。 轉換會先以 Y 方向的 0.5 乘以 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.

注意

在上述範例中,橢圓形會旋轉座標系統的原點,而座標系統位於工作區左上角。 這會產生與旋轉其中心橢圓形不同的結果。

本機轉換

本機轉換會套用至要繪製的特定專案。 例如, 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.

接下來,假設您需要在右側執行 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.

另請參閱