共用方式為


使用全局轉換

全局轉換是 Graphics 類別的屬性。 指定全局轉換的數字是儲存在 Matrix 物件中,這個物件代表 3×3 的矩陣。 MatrixGraphics 類別提供了幾個方法,可用來設定全局轉換矩陣中的數字。

不同類型的轉換

在下列範例中,程式碼會先建立 50×50 的矩形,並將它放在原點 (0, 0)。 原點是指工作區 (Client Area) 左下角的位置。

        Dim rect As New Rectangle(0, 0, 50, 50)
        Dim pen As New Pen(Color.FromArgb(128, 200, 0, 200), 2)
        e.Graphics.DrawRectangle(pen, rect)

Rectangle rect = new Rectangle(0, 0, 50, 50);
Pen pen = new Pen(Color.FromArgb(128, 200, 0, 200), 2);
e.Graphics.DrawRectangle(pen, rect);

下列程式碼會套用縮放轉換,將矩形的 x 方向以 1.75 因數放大,將矩形的 y 方向以 0.5 因數縮小:

        e.Graphics.ScaleTransform(1.75F, 0.5F)
        e.Graphics.DrawRectangle(pen, rect)

e.Graphics.ScaleTransform(1.75f, 0.5f);
e.Graphics.DrawRectangle(pen, rect); 

產生矩形的 x 方向比原始矩形長,y 方向則比原始矩形短。

若要旋轉矩形,而非縮放矩形,請使用下列程式碼:

        e.Graphics.ResetTransform()
        e.Graphics.RotateTransform(28) ' 28 degrees
        e.Graphics.DrawRectangle(pen, rect)

e.Graphics.ResetTransform();
e.Graphics.RotateTransform(28); // 28 degrees
e.Graphics.DrawRectangle(pen, rect);

若要轉換矩形,請使用下列程式碼:

        e.Graphics.ResetTransform()
        e.Graphics.TranslateTransform(150, 150)
        e.Graphics.DrawRectangle(pen, rect)

e.Graphics.ResetTransform();
e.Graphics.TranslateTransform(150, 150);
e.Graphics.DrawRectangle(pen, rect);

請參閱

參考

Matrix

其他資源

座標系統和轉換

使用 Managed GDI+ 中的轉換