HOW TO:旋轉、反射和傾斜影像
您可以指定原始影像左上角、右上角和左下角的目的座標位置,即可旋轉、反射和傾斜影像。 這三個目的座標位置會決定原始的矩形影像對應至平形四邊形的仿射轉換。
範例
例如,假設原始影像是左上角位於 (0, 0)、右上角位於 (100, 0) 和左下角位於 (0, 50) 的矩形。 現在,假設要依照下列方式,將這三個點對應至目的座標位置。
原始座標位置 |
目的座標位置 |
---|---|
左上角 (0, 0) |
(200, 20) |
右上角 (100, 0) |
(110, 100) |
左下角 (0, 50) |
(250, 30) |
下圖顯示的是原始的影像和對應至平形四邊形的影像。 原始影像已經過傾斜、反射、旋轉和轉換。 原始影像上邊緣的 X 軸會對應至連接 (200, 20) 和 (110, 100) 的線條。 原始影像左邊緣的 Y 軸則會對應至連接 (200, 20) 和 (250, 30) 的線條。
下圖顯示的是套用於相片影像的類似轉換。
下圖顯示的是套用於中繼檔的類似轉換。
下列範例會產生第一個圖中所顯示的影像。
' New Point(200, 20) = destination for upper-left point of original
' New Point(110, 100) = destination for upper-right point of original
' New Point(250, 30) = destination for lower-left point of original
Dim destinationPoints As Point() = { _
New Point(200, 20), _
New Point(110, 100), _
New Point(250, 30)}
Dim image As New Bitmap("Stripes.bmp")
' Draw the image unaltered with its upper-left corner at (0, 0).
e.Graphics.DrawImage(image, 0, 0)
' Draw the image mapped to the parallelogram.
e.Graphics.DrawImage(image, destinationPoints)
Point[] destinationPoints = {
new Point(200, 20), // destination for upper-left point of
// original
new Point(110, 100), // destination for upper-right point of
// original
new Point(250, 30)}; // destination for lower-left point of
// original
Image image = new Bitmap("Stripes.bmp");
// Draw the image unaltered with its upper-left corner at (0, 0).
e.Graphics.DrawImage(image, 0, 0);
// Draw the image mapped to the parallelogram.
e.Graphics.DrawImage(image, destinationPoints);
編譯程式碼
上述範例是專為與 Windows Form 搭配使用而設計的,而且它需要 PaintEventArgs e (即 Paint 事件處理常式的參數)。 請確定以系統中有效的影像路徑來取代 Stripes.bmp。