0

What I want is to move the object and rotate along its center point. I've used Matrix class for transformation:

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.ResetTransform();
        Matrix transformationMatrix = new Matrix();
        transformationMatrix.RotateAt(rot, new PointF(img.Size.Width / 2, img.Size.Height / 2));
        e.Graphics.Transform = transformationMatrix;
        e.Graphics.DrawImage(img, 0, 0, img.Size.Width, img.Size.Height);
    }

Above code will rotate the image along its center.

But if I try to move it(I placed the image in the center of pictureBox), the image is no more rotating along it's center point.

e.Graphics.DrawImage(img, (pictureBox1.Width - img.Size.Width) / 2, (pictureBox1.Height - img.Size.Height) / 2, img.Size.Width, img.Size.Height);

Now I guess I have to use Translate function to specify position, but I have no clue how to do that. Translate takes relative position. I want to specify the image location using its center point and be able to rotate it along its center.

Update 2:

Modified code look like this

origin.X = 50;
origin.Y = 50;

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.TranslateTransform(origin.X, origin.Y);
        e.Graphics.RotateTransform(rot);
        e.Graphics.DrawImage(img, -img.Size.Width, -img.Size.Height/2, img.Size.Width, img.Size.Height);
    }

So I defined Point origin to specify the location of my image. But still it doesn't rotate along its center.

enter image description here

Pablo
  • 28,133
  • 34
  • 125
  • 215

1 Answers1

1

Yes, you want to use the Translate function. Here is an example I wrote for another question that shows how to translate and rotate and image:

https://stackoverflow.com/a/10956388/351385

Updated

What you want to do is set the translation point to the point in the window where the center of your object will be. This causes the [0, 0] point of the display to become that point, so any rotation will happen around it. Then when you draw the image, use the mid-point of the image [image width / 2, image height / 2] as coordinates to the DrawImage method.

Updated again

Sorry, the coordinates passed to DrawImage are the negated mid-points of the image [0 - width / 2, 0 - height / 2].

Community
  • 1
  • 1
Tergiver
  • 14,171
  • 3
  • 41
  • 68
  • see update 2. It looks like in my case it's more difficult to achieve. Basically the image is the line located in the square frame vertically, centered horizontally(can post it if description not clear). The red circle is midpoint of 320x320 square. – Pablo Mar 14 '13 at 21:51
  • oops sorry, please ignore, there was a typо in the code, just noticed. – Pablo Mar 14 '13 at 22:04