0

So I have a picturebox with a background image of size 123x123 pixels.

Everytime the image is clicked I make it rotate by a certain angle using the function below.

//The original image to rotate.
private readonly Bitmap _origPowerKnob = Properties.Resources.PowerKnob;

//Calling the rotation function.
using (Bitmap b = new Bitmap(_origPowerKnob))
{
    Bitmap newBmp = RotateImage(b, _powerAngle);
    PowerKnob.BackgroundImage = newBmp;
}

//Do the rotation.
private 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.
    Graphics g = Graphics.FromImage(returnBitmap);
    //move rotation point to center of image.
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.TranslateTransform(b.Width / 2F, b.Height / 2F);
    //Rotate.
    g.RotateTransform(angle);
    //Move image back.
    g.TranslateTransform(-b.Width / 2F, -b.Height / 2F);
    //Draw passed in image onto graphics object.
    g.DrawImage(b, new PointF(0,0));
    return returnBitmap;
}

enter image description here

The problem is the image in not correctly rotating around the center. It is a little off and I can't seem to understand why. Any suggestions?

AgentFire
  • 8,944
  • 8
  • 43
  • 90
Nolemonpledge
  • 139
  • 1
  • 2
  • 11

0 Answers0