全域和區域變換
全域變換是一種套用到指定 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)
[C#]
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 方法可用來變換該路徑的資料點 (Data Point)。下列範例繪製未變換的矩形和經過旋轉和變換的路徑 (假設沒有自然變換)。
Dim myMatrix As New Matrix()
myMatrix.Rotate(45)
myGraphicsPath.Transform(myMatrix)
myGraphics.DrawRectangle(myPen, 10, 10, 100, 50)
myGraphics.DrawPath(myPen, myGraphicsPath)
[C#]
Matrix myMatrix = 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 物件的自然變換。
Dim myMatrix As New Matrix(1, 0, 0, - 1, 0, 0)
myGraphics.Transform = myMatrix
myGraphics.TranslateTransform(200, 150, MatrixOrder.Append)
[C#]
Matrix myMatrix = new Matrix(1, 0, 0, -1, 0, 0);
myGraphics.Transform = myMatrix;
myGraphics.TranslateTransform(200, 150, MatrixOrder.Append);
下列程式碼 (位於上述範例的結束處) 建立單一矩形組成的路徑,其中該矩形的左上角位於新座標系統的原點。該矩形將填入色彩兩次,其中一次並未使用區域變換,另外一次則使用區域變換。區域變換包括水平縮放 2 個係數,隨後進行 30 度的旋轉。
' 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)
[C#]
// 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);
下圖將顯示新的座標系統和兩個矩形。