如何:旋转、反射和扭曲图像

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

示例

例如,假设原始图像是一个矩形,其左上角坐标为 (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) 的线。

The original image and the image mapped to the parallelogram.

下图显示应用于照片图像的类似变换:

The picture of a climber and the picture mapped to the parallelogram.

下图显示应用于元文件的类似变换:

Illustration of shapes and text and that mapped to the parallelogram.

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

    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 替换为系统中有效的图像路径。

另请参阅