如何旋转、翻转和倾斜图像

可以通过为原始图像的左上角、右上角和左下角指定目标点来旋转、反射和倾斜图像。 三个目标点确定了一个仿射变换,将原始矩形图像映射到一个平行四边形。

示例:

例如,假设原始图像是左上角为(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) 的线条。

原始图像和映射到平行四边形的图像。

下图显示了应用于摄影图像的类似转换:

登山者的图片和映射到平行影像的图片。

下述示例显示了应用于元文件的类似转换。

形状和文本的插图被映射到平行四边形上。

以下示例生成第一张图中显示的图像。

    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);
' 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)

编译代码

前面的示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 请确保将 Stripes.bmp 替换为您系统上有效的图像路径。

另请参阅