How can I rotate an RectangleF at a specific degree using Graphics object?

笑着哭i 提交于 2019-12-23 15:13:53

问题


I have tried this:

g.RotateTransform(degrees);

But nothing happens.I have one graphics object and one rectangle object witch im drawing using this method:

g.FillRectangle(new TextureBrush(Image.FromFile(@"D:\LOVE&LUA\Pictures\yellowWool.png")), rectangle);

And i need to rotate the rectangle somehow and draw it again.

Answer with code sample please and with a simple explanation.

EDIT: Here is the actual code I'm using:

        public void Draw(Graphics g,PointF location,Color clearColor)
    {
        rectangle.Location = location;
        g.Clear(clearColor);
        g.RotateTransform(10);
        //g.FillRectangle(new SolidBrush(Color), rectangle);
        g.FillRectangle(new TextureBrush(Image.FromFile(@"D:\LOVE&LUA\Pictures\yellowWool.png")), rectangle);
    }

Each frame I call this function and I'm using form's Paint event's e.Graphics object for the Graphics and i have a timer witch only calls this.Refresh();

EDIT 2: OK I have played a little with the transformations and g.RotateTransform rotates the whole cordinate system of the graphycs object and i need to rotate only the rectangle without changing the cordinate system


回答1:


You can try using a matrix with the RotateAt method to center the rotation around the rectangle:

using (Matrix m = new Matrix()) {
  m.RotateAt(10, new PointF(rectangle.Left + (rectangle.Width / 2),
                            rectangle.Top + (rectangle.Height / 2)));
  g.Transform = m;
  using (TextureBrush tb = new TextureBrush(Image.FromFile(@"D:\LOVE&LUA\Pictures\yellowWool.png"))
    g.FillRectangle(tb, rectangle);
  g.ResetTransform();
}

The ResetTransform() will turn the graphics back to normal processing after that.



来源:https://stackoverflow.com/questions/8189383/how-can-i-rotate-an-rectanglef-at-a-specific-degree-using-graphics-object

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