全域和區域轉換
更新:2007 年 11 月
全域轉換會套用至特定 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);
下圖將顯示轉換所使用的矩陣。
![]() |
---|
在上述範例中,橢圓形會繞著座標系統的原點旋轉,該原點位於工作區 (Client Area) 的左上角。這項作業產生的結果和在橢圓形中心點旋轉並不相同。 |
區域轉換
區域轉換會套用至要繪製的特定項目。例如,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)
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)
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)
// 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);
下圖將顯示新的座標系統和兩個矩形。