C # rotating graphics?

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); } } 

But what should I do to rotate it?

Thanks!

+8
c #
source share
5 answers
 public static Bitmap RotateImage(Bitmap b, float angle) { //create a new empty bitmap to hold rotated image Bitmap returnBitmap = new Bitmap(b.Width, b.Height); //make a graphics object from the empty bitmap using(Graphics g = Graphics.FromImage(returnBitmap)) { //move rotation point to center of image g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2); //rotate g.RotateTransform(angle); //move image back g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2); //draw passed in image onto graphics object g.DrawImage(b, new Point(0, 0)); } return returnBitmap; } 
+22
source share

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

Graphics.DrawImage method (image, dot [])

Notes

The destPoints parameter indicates three points of the parallelogram. the three point structures are the upper left, upper right, and lower left corners of the parallelogram. The fourth paragraph is extrapolated from the first three to form a parallelogram.

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

MSDN also has an article describing the use of this method: How to rotate, flip, and skew images using the following code example. Unfortunately, this example complicates the problem, also distorting 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:

  • This method does not allow you to specify the angle of rotation in degrees - you need to use simple trigonometry to obtain points.
  • This conversion applies only to a specific image.
    • Well, if you only need to draw one rotated image, and everything else does not rotate, because after that you do not have to reset Graphics.Transform .
    • It’s bad if you want to rotate several things together (ie rotate the “camera”).
+7
source share

Use Graphics.RotateTransform to rotate the image.

 protected override void OnPaint(PaintEventArgs e) { var tempRocket = new Bitmap(Properties.Resources.rocket); e.Graphics.RotateTransform(30.0F); e.Graphics.DrawImage(tempRocket, 150, 150); } 
+2
source share

You need to apply a transformation matrix. Here you can find a good example about conversions in GDI +

0
source share

No crop version:

 private Bitmap RotateBitmap(Bitmap bitmap, float angle) { int w, h, x, y; var dW = (double)bitmap.Width; var dH = (double)bitmap.Height; double degrees = Math.Abs(angle); if (degrees <= 90) { double radians = 0.0174532925 * degrees; double dSin = Math.Sin(radians); double dCos = Math.Cos(radians); w = (int)(dH * dSin + dW * dCos); h = (int)(dW * dSin + dH * dCos); x = (w - bitmap.Width) / 2; y = (h - bitmap.Height) / 2; } else { degrees -= 90; double radians = 0.0174532925 * degrees; double dSin = Math.Sin(radians); double dCos = Math.Cos(radians); w = (int)(dW * dSin + dH * dCos); h = (int)(dH * dSin + dW * dCos); x = (w - bitmap.Width) / 2; y = (h - bitmap.Height) / 2; } var rotateAtX = bitmap.Width / 2f; var rotateAtY = bitmap.Height / 2f; var bmpRet = new Bitmap(w, h); bmpRet.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution); using (var graphics = Graphics.FromImage(bmpRet)) { graphics.Clear(Color.White); graphics.TranslateTransform(rotateAtX + x, rotateAtY + y); graphics.RotateTransform(angle); graphics.TranslateTransform(-rotateAtX - x, -rotateAtY - y); graphics.DrawImage(bitmap, new PointF(0 + x, 0 + y)); } return bmpRet; } 

Main source

0
source share

All Articles