Why might Graphics.RotateTransform() not be applied?

偶尔善良 提交于 2019-12-25 03:49:17

问题


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?


回答1:


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.




回答2:


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).



来源:https://stackoverflow.com/questions/4318073/why-might-graphics-rotatetransform-not-be-applied

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!