Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating graphics?

Tags:

c#

I have this code, that draws an image.

private void timer1_Tick(object sender, EventArgs e)
{
    Invalidate();
}

protected override void OnPaint(PaintEventArgs e)
{
    var tempRocket = new Bitmap(Properties.Resources.rocket);

    using (var g = Graphics.FromImage(tempRocket))
    {
        e.Graphics.DrawImage(tempRocket, 150, 150);
    }
}

Yet what do I do to rotate it?

like image 619
Rob Avatar asked Mar 02 '11 20:03

Rob


People also ask

What is rotation in graphic design?

It is a process of changing the angle of the object. Rotation can be clockwise or anticlockwise. For rotation, we have to specify the angle of rotation and rotation point. Rotation point is also called a pivot point.

What is a rotating image?

When referring to an image or image editor, rotate is a feature that lets you turn an image in a clockwise or counterclockwise direction. For example, many editors allow you to rotate images 90, 180, or 270.

What is 2D rotation in computer graphics?

In. Computer graphics, 2D Rotation is a process of rotating an object with respect to an angle in a two dimensional plane. Consider a point object O has to be rotated from one angle to another in a 2D plane.


1 Answers

There are overloads of Graphics.DrawImage that take an array of three points used to define a parallelogram for the destination, such as:

Graphics.DrawImage Method (Image, Point[])

Remarks

The destPoints parameter specifies three points of a parallelogram. The three Point structures represent the upper-left, upper-right, and lower-left corners of the parallelogram. The fourth point is extrapolated from the first three to form a parallelogram.

The image represented by the image parameter is scaled and sheared to fit the shape of the parallelogram specified by the destPoints parameters.

There is also an article on MSDN describing the use of this method: How to: Rotate, Reflect, and Skew Images, with the following code example. Unfortunately, the example complicates the issue by also skewing the image.

Point[] destinationPoints = {
          new Point(200, 20),   // destination for upper-left point of original
          new Point(110, 100),  // destination for upper-right point of original
          new Point(250, 30)};  // destination for lower-left point of original

Image image = new Bitmap("Stripes.bmp");

// Draw the image unaltered with its upper-left corner at (0, 0).
e.Graphics.DrawImage(image, 0, 0);

// Draw the image mapped to the parallelogram.
e.Graphics.DrawImage(image, destinationPoints);

The main differences compared to using the Graphics.Transform property are:

  • This method does not allow you to specify the rotation angle in degrees -- you have to use some simple trigonometry to derive the points.
  • This transformation applies only to the specific image.
    • Good if you only need to draw one rotated image and everything else is non-rotated since you don't have to reset Graphics.Transform afterward.
    • Bad if you want to rotate several things together (i.e., rotate the "camera").
like image 50
Justin Avatar answered Sep 26 '22 08:09

Justin