3 Replies - 3742 Views - Last Post: 25 June 2012 - 10:38 PM Rate Topic: -----

#1 jp612   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 16-August 11

Rotating multiple images around each other with C#. Custom Control.

Posted 25 June 2012 - 07:04 PM

Hi,

Attached Image

In the attached image I have 3 separate images which are drawn: the outer arrow which rotates around the circle to point at different directions (like a compass) the outer circle which has a notch and rotates to point the notch some where like the arrow. and the inner circle which is supposed to spin at certain times.

I want to create an animation so that the notch circle and the arrow rotate around to the right positions which are every 45 degrees. (like a compass with N, E, S, W and NE, NW,SE and SW). But I'm trying to get them to rotate in a smooth motion until they stop.

Im doing this in a C# .net custom control.

here is the code I have to draw the images in place so far:

namespace CompassRing
{
    public partial class CompassRing : Control
    {
        private int rWidth, rHeight, lrWidth, lrHeight, arWidth, arHeight;
        private int rX, rY;
        Assembly _assembly;
        
        public CompassRing()
        {
            InitializeComponent();

            _assembly = Assembly.GetExecutingAssembly();

            Bitmap bm = new Bitmap(_assembly.GetManifestResourceStream("CompassRing.Resources.Compass-Ring.png"));
            rWidth = bm.Width;
            rHeight = bm.Height;
            bm = new Bitmap(_assembly.GetManifestResourceStream("CompassRing.Resources.Compass-LoadingRing-default.png"));
            lrWidth = bm.Width;
            lrHeight = bm.Height;
            bm = new Bitmap(_assembly.GetManifestResourceStream("CompassRing.Resources.Compass-Arrow.png"));
            arWidth = bm.Width;
            arHeight = bm.Height;

            rX = 0;
            rY = 0;

            bm.Dispose();
        }

        protected void RotateRing(PaintEventArgs pe)
        {
            for (int i = 0; i <= 360; i++)
            {
                
            }
        
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Bitmap bm;

            bm = new Bitmap(_assembly.GetManifestResourceStream("CompassRing.Resources.Compass-Ring.png"));          
            pe.Graphics.DrawImageUnscaled(bm, rX + (arWidth / 2), rY);
            
            Point lrDrawPoint = new Point((rWidth / 2) - (lrWidth / 2) + (arWidth / 2) - (arWidth / 5),(rHeight / 2) - (lrHeight / 2));
            bm = new Bitmap(_assembly.GetManifestResourceStream("CompassRing.Resources.Compass-LoadingRing-default.png"));
            pe.Graphics.DrawImageUnscaled(bm, lrDrawPoint);

            Point arrDrawPoint = new Point(0,(rHeight / 7));
            bm = new Bitmap(_assembly.GetManifestResourceStream("CompassRing.Resources.Compass-Arrow.png"));
            pe.Graphics.DrawImageUnscaled(bm, arrDrawPoint);

            bm.Dispose();

  
        }
    }
}



So when I have drawn them I need to rotate them but then not re-run the code for the initial drawing and redraw every few degrees using a timer or something right? How do I do the rotataion? I have tried some stuff by using the following example but its come out all smeared so far:

using (Graphics gfx = this.CreateGraphics())
{
    for (int i = 0; i <= 360; i++)
    {       
        Bitmap b = new Bitmap(m.Width, m.Height);

        using (Graphics g = Graphics.FromImage(B)/>)
        {
            g.TranslateTransform(m.Width / 2, m.Height / 2);
            g.RotateTransform(i);
            g.TranslateTransform(-m.Width / 2, -m.Height / 2);
            g.DrawImage(m, 0, 0);             
        }
        
       gfx.DrawImage(b, 100, 100);
       b.Dispose();
    }
}



I just used this part:

using (Graphics g = Graphics.FromImage(B)/>)
        {
            g.TranslateTransform(m.Width / 2, m.Height / 2);
            g.RotateTransform(i);
            g.TranslateTransform(-m.Width / 2, -m.Height / 2);
            g.DrawImage(m, 0, 0);             
        }



and just made the rotation and stuff using my pe.Graphics object. using the notch ring to try it. But I only used one graphics object, because I don't get how i can use more than one? can't there only be one drawing object, drawing at any given time?

So this doesn't really work the way I have tried it.

Also if I want both the arrow and notch circle to rotate at the same time. How could I do this?

I cant really get anything to work at this point and I have even tried to use the timer, just rotate my images to a different position first. But I cant get that to work either.

Any ideas??

This post has been edited by jp612: 25 June 2012 - 07:13 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Rotating multiple images around each other with C#. Custom Control.

#2 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: Rotating multiple images around each other with C#. Custom Control.

Posted 25 June 2012 - 08:13 PM

if you said you have 3 images:
arrow - (rotate around notch circle)
notch circle - rotate around center
inner circle - rotate around center

then you can use 3 graphics object and make rotation for each image and draw them to the same image at the same time.
Was This Post Helpful? 0
  • +
  • -

#3 jp612   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 16-August 11

Re: Rotating multiple images around each other with C#. Custom Control.

Posted 25 June 2012 - 09:18 PM

View Postsela007, on 25 June 2012 - 08:13 PM, said:

if you said you have 3 images:
arrow - (rotate around notch circle)
notch circle - rotate around center
inner circle - rotate around center

then you can use 3 graphics object and make rotation for each image and draw them to the same image at the same time.


ok sweet. Ill do that now. So if i just make my 3 graphics objects i can just use the Graphics.DrawImage() for each one and they will all just draw to the canvas as long as its in the paint method right?

Do i even have to use the PaintEventArgs pe? or just make my own graphics objects and ignore it?
Was This Post Helpful? 0
  • +
  • -

#4 sela007   User is offline

  • D.I.C Addict

Reputation: 139
  • View blog
  • Posts: 841
  • Joined: 21-December 11

Re: Rotating multiple images around each other with C#. Custom Control.

Posted 25 June 2012 - 10:38 PM

Quote

So if i just make my 3 graphics objects i can just use the Graphics.DrawImage() for each one and they will all just draw to the canvas as long as its in the paint method right?

yes i think this should work, after you create 3 bitmaps, rotate them separately, and draw on them arrow,circle and inner circle, just paste that 3 bitmaps on the new image using another graphics object and DrawImage method.

Quote

Do i even have to use the PaintEventArgs pe? or just make my own graphics objects and ignore it?
you can use picturebox and draw images with your own graphics objects.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1