0

I need to display an image which is rotating continuously until that form closes. This means that the image should keep on rotating. I am trying this sample in windows application with c#. I did an example that text in the label is moving by the following code:

label1.Location = new Point(label1.Location.X + 5, label1.Location.Y);

It may be the text in the label must rotate not like moving horizontally or vertically.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user2897388
  • 85
  • 4
  • 16
  • 1
    This is what WPF is dessigned for, I think. – Manu343726 Dec 24 '13 at 09:35
  • not in WPF.Using Windows applications – user2897388 Dec 24 '13 at 09:37
  • 1
    Then perhaps you *should* try WPF to do this. In WPF you only need to add a rotation transform to the label. In WinForms you need to draw the string using GDI+, taking care to delete any previous text strings before drawing the new one, avoid flickering by using double buffering etc – Panagiotis Kanavos Dec 24 '13 at 09:40

3 Answers3

2

You can do it with the following steps:

  • add an angle variable

  • add a "canvas" control to your form, e.g. a Panel

  • override its Paint event

  • obtain its Graphics contex and set a rotation transform. You can use Rotation method supplying the value of the angle variable.

  • use label's DrawToBitmap method, to render your label onto a panel

  • modify angle and call first the Invalidate and then Update on the control that owns the canvas you're drawing on (in this example - the Panel)

Your label shouldn't be added in the designer.

You can also use it without a label control is a simple text will suffice - you can just use the DrawString method then.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
1

Try something like this:

<Storyboard x :Key="sbSpin">
<DoubleAnimation
  Storyboard.TargetName="Spin"
  Storyboard.TargetProperty="(Button.RenderTransform).(RotateTransform.Angle)"
  From="0"
  To="360"
  RepeatBehavior="Forever"
  Duration="0:0:3" Completed ="DoubleAnimation_Completed" />
</Storyboard>

I wrote a series of blog posts on this recently:

http://pmichaelsdev.wordpress.com/2013/11/18/animation-in-xaml-part-3/

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
0

There are plenty of articles which explain how to rotate Image. You need some more customizations to keep it rotating while your form is loaded.

Just download the source code and add following piece of code in Form1. Start a new thread and call a function which keeps rotating the image.

       private void Form1_Load(object sender, EventArgs e)
        {
            Thread t = new Thread(KeepRotating);   // Kick off a new thread
            t.Start();
        }

       void KeepRotating()
        {
            for (float i = 1; i <= 360; i++)
            {
                RotateImage(pictureBox1, image, i);
                Thread.Sleep(20);  // for slower rotation, Avoid if not needed
                if (i == 360f)
                {
                    i = 1f;
                }
            }
        }

Output looks something like this.

enter image description here

P.S. This is a very simple and raw method to accomplish the task. Make sure you take care of exception handling and memory leaks.

Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116