为什么不能应用 Graphics.RotateTransform() ?

发布于 2024-10-05 15:18:15 字数 511 浏览 15 评论 0原文

我有以下功能:

static private Image CropRotate(Image wholeImage, Rectangle cropArea)
{
    Bitmap cropped = new Bitmap(cropArea.Width, cropArea.Height);

    using(Graphics g = Graphics.FromImage(cropped))
    {
        g.DrawImage(wholeImage, new Rectangle(0, 0, cropArea.Width, cropArea.Height), cropArea, GraphicsUnit.Pixel);
        g.RotateTransform(180f);
    }
    return cropped as Image;
}

它应该裁剪图像,然后旋转生成的子图像。但实际上,它只执行裁剪。

为什么没有应用RotateTransform()

I have the following function:

static private Image CropRotate(Image wholeImage, Rectangle cropArea)
{
    Bitmap cropped = new Bitmap(cropArea.Width, cropArea.Height);

    using(Graphics g = Graphics.FromImage(cropped))
    {
        g.DrawImage(wholeImage, new Rectangle(0, 0, cropArea.Width, cropArea.Height), cropArea, GraphicsUnit.Pixel);
        g.RotateTransform(180f);
    }
    return cropped as Image;
}

It's supposed to crop an image, then rotate the resulting sub-image. In actuality though, it only performs the crop.

Why is RotateTransform() not being applied?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮倦 2024-10-12 15:18:15

您是否尝试过将 RotateTransform() 放在 DrawImage() 之前?
msdn 页面上的示例 显示了在进行任何绘图之前应用的转换完毕。

Have you tried putting the RotateTransform() before the DrawImage()?
The example on the msdn page shows the transformation being applied before any drawing is done.

猥琐帝 2024-10-12 15:18:15

RotateTransform 调用会更改当前的变换矩阵,这会对所有后续操作产生影响。它根本转换已经输出的操作。这对于任何更改变换矩阵的操作(例如 ScaleTransform )都是相同的。

确保在执行要转换的操作之前调用这些函数 - 在本例中,是在调用 DrawImage 之前。

您可以使用它来执行诸如

  1. 绘制(不旋转或缩放)
  2. 旋转(仅更改变换矩阵)
  3. 缩放(仅更改变换矩阵)
  4. 绘制(现在旋转和缩放)
  5. ClearTransform(仅更改变换矩阵)
  6. 绘制(不旋转或缩放)之类的操作

第一个和最后一个绘制输出不会被转换,但中间的输出将受到旋转和缩放(按顺序)的影响。

The RotateTransform call alters the current transform matrix, which has an effect on all subsequent operations. It does not transform the already output operations at all. This is the same for any of the operations that change the transform matrix (like ScaleTransform).

Make sure you call these before you perform the operations you want transformed - in this case, before the call to DrawImage.

You can use this to do something like

  1. Draw (not rotated or scaled)
  2. Rotate (only changes transform matrix)
  3. Scale (only changes transform matrix)
  4. Draw (now rotated and scaled)
  5. ClearTransform (only changes transform matrix)
  6. Draw (not rotated or scaled)

the first and last draw outputs will not be transformed, but the middle one would be affected by both the rotate and scale (in that order).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文