全域和區域轉換

全域轉換是套用至指定 Graphics 物件所繪製之每個項目的轉換。 相反地,區域轉換是套用至要繪製之特定項目的轉換。

全域轉換

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

下圖顯示轉換所涉及的矩陣。

縮放、平移和旋轉矩陣結合以形成全域轉換的圖例。

注意

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

區域轉換

區域轉換會套用至要繪製的特定項目。 例如,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 軸指向下,因此您必須跨水平軸執行反射。 下圖顯示這類反射的矩陣。

跨水平軸反射之矩陣的圖例。

接下來,假設您需要在右側平移 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)

下圖顯示新的座標系統和兩個矩形。

新座標系統和兩個矩形的圖例。

另請參閱