Bosak
Bosak

Reputation: 2153

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

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

Upvotes: 4

Views: 2880

Answers (1)

LarsTech
LarsTech

Reputation: 81675

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.

Upvotes: 1

Related Questions