Dan

Dan

  • NA
  • 35
  • 0

Rotate Image on Itself With Matrix Object?

Feb 27 2011 12:59 PM

Hi guys, 

I hope you can help?

I'm trying to rotate a circle graphically in C# so that it just spins round (with no movement side to side or up or down) just like a wheel does.

I'm using the Matrix.Rotate(), Matrix.Translate() and Graphics.Transform() methods. I can get the object to rotate no problem but not so that it stays in one place & rotates on itself. I think it's probably just a case of getting the correct parameters for the Matrix.Translate() method. My best guess for it so far is below:

(It's a try at using the Rectangle object I used to create my Ellipse drawing to try & centre the rotation)

matrix1.Translate((float)rectangle1.Width / 2, (float)rectangle1.Height / 2);

I understand well all the basics about Graphics, Image, Bitmap, Rectangle and Brush objects etc and their associated methods. However, if honest, the Matrix class & various Rotation & Transform methods are new to me. I've been searching the web & feel like I've tried everything without success.

Below is an abbreviation of my best code so far. It rotates the image but not on itself. Any help is very much appreciated:

(Note: I've tried the simpler Graphics.TranslateTransform() and Graphics. RotateTransform() but unfortunately these don't seem to work with the Graphics.FillEllipse() method)

public Form1()

        {

            InitializeComponent();

            CreateCircleSupportElements();

        }

        Graphics graphics1;

        Image image1;

        Brush brush1;

        Rectangle rectangle1;

        Matrix matrix1;

 

        private void Form1_Paint(object sender, PaintEventArgs e)

        {

            graphics1 = e.Graphics;

            graphics1.Transform = matrix1;

            graphics1.FillEllipse(brush1, rectangle1);

        }

        private void CreateCircleSupportElements()

        {

            image1 = Image.FromFile(@"C:\APicture.jpg");

            brush1 = new TextureBrush(image1);

            rectangle1 = new Rectangle(100, 100, 100, 100);

            matrix1 = new Matrix(); 

        }

        private void button1_Click(object sender, EventArgs e)

        {

            matrix1.Translate((float)rectangle1.Width / 2,                 (float)rectangle1.Height / 2);

            matrix1.Rotate(5);

            this.Invalidate();

        }

Thank you,

Dan.


Answers (3)