How can I rotate RectangleF to a certain extent using a Graphics object?

I tried this:

g.RotateTransform(degrees); 

But nothing happens. I have one graphic object and one object with a rectangle created using this method:

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

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

Answer the sample code, 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); } 

Every frame I call this function, and I use the Paint e.Graphics form object for Graphics, and I have a timer witch only calls this.Refresh();

EDIT 2: OK I played a little with transformations, and g.RotateTransform rotates the entire core system of the graphycs object, and I need to rotate only the rectangle without changing the core system

+4
source share
1 answer

You can try using the matrix using 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(); } 

After that, ResetTransform() will return the graphics to normal processing again.

+1
source

Source: https://habr.com/ru/post/1381975/


All Articles