0

I have a picturebox with an image in it. The image contains two ellipses face to face (black & blue).

What I want is to rotate the picturebox in a timer (for the effect) so the image to be "upside down" would look much more like they've changed place, which basically it's just rotating the picturebox like how the erath is moving around it's axis.

TaW
  • 53,122
  • 8
  • 69
  • 111
Laminores
  • 19
  • 2
  • Flipping it is easy (non-animated, just swapping Y coordinates), rotating through the Z axis (as if you put your finger on the screen and spun it around it) is easy, however to do what you are asking requires a 3-D transformation (rotating around the Y axis) and isn't supported by anything built-in. WPF can do it, you could use a WPF control inside a WinForms application instead of a picturebox... – Ron Beyer Aug 18 '15 at 19:32
  • I know, but i would like it in windows form with picturebox...I used the parametric eqation for moving around a radius the picturebox within a timer, but basically what i want is to spin it like that. – Laminores Aug 18 '15 at 19:34
  • rotating through a Z- axys, do you have any code sample? – Laminores Aug 18 '15 at 19:36
  • http://stackoverflow.com/questions/4144106/rotate-image-in-picturebox – Ron Beyer Aug 18 '15 at 19:44
  • But the control itself cannot be rotated by adjusting the location points within a timer? – Laminores Aug 18 '15 at 19:51
  • No, it doesn't work like that, location is simply the x/y location of the picture box, it doesn't have any methods to rotate the box around another point. – Ron Beyer Aug 18 '15 at 19:54
  • Well if you adjust them like the y to decreese and x to increese they can give a rotating effect. – Laminores Aug 18 '15 at 19:58
  • 1
    I'd suggest you try it and realize that its not a rotation, its more like moving the box in a circle, it doesn't rotate the image. – Ron Beyer Aug 18 '15 at 20:00
  • possible duplicate of [How to rotate image in picture box](http://stackoverflow.com/questions/26450764/how-to-rotate-image-in-picture-box) – TaW Aug 19 '15 at 05:53
  • See [here](http://stackoverflow.com/questions/26525965/rotate-a-graphics-bitmap-at-its-center/26527737?s=1|2.3272#26527737) or [here](http://stackoverflow.com/questions/26450764/how-to-rotate-image-in-picture-box/26455088?s=4|1.4338#26455088) for examples that actually work and are written in C# – TaW Aug 19 '15 at 05:53
  • Have you resolved your problems? – TaW Sep 02 '15 at 13:42
  • well not quiet, seems like i can rotate only the image within the control, and not the control itself, like how this has been made: http://www.codeproject.com/Articles/18076/A-Panel-control-that-you-can-flip-over – Laminores Sep 02 '15 at 13:44

1 Answers1

1

There are various kinds of rotations from a globe, depending on how you look at it.

If you look at it from above the poles it spins like a disk or a gear and you can find code for it here. This has the advantage that you can use any image and rotate it.

If you look at it from the side, facing the equator you can't easily use bitmaps, but using just two colors it will still look nice..

Here is an example of a 'globe-like' spinning rotation:

enter image description hereenter image description hereenter image description here

float angle = 0f;
float aSpeed = 4f;                      // <-- set your speed
Brush brush1 = Brushes.CadetBlue;       // and your..
Brush brush2 = Brushes.DarkSlateBlue;   // ..colors

private void timer1_Tick(object sender, EventArgs e)
{
    angle += aSpeed;
    if (angle + aSpeed > 360)
    {
        angle -= 360f;
        Brush temp = brush1;
        brush1 = brush2;
        brush2 = temp;
    }
    pictureBox1.Invalidate();
}

private void pictureBox1_Click(object sender, EventArgs e)
{
    timer1.Enabled = !timer1.Enabled;
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    Rectangle r = pictureBox1.ClientRectangle;
    Rectangle r2 = r;   // see below..
    r.Inflate(-20, -20);     // a little smaller than the panel or pBox

    if (angle < 180)
    {
        e.Graphics.FillEllipse(brush1, r);
        e.Graphics.FillPie(brush2, r, 270, 180);
        r.Inflate(-(int)(r.Width * angle / 360f), 0);
        e.Graphics.FillEllipse(brush2, r);
    }
    else
    {
        e.Graphics.FillEllipse(brush2, r);
        e.Graphics.FillPie(brush1, r, 90, 180);
        r.Inflate(-(int)(r.Width * angle / 360f), 0);
        e.Graphics.FillEllipse(brush1, r);
    }
}

}

This is created by three DrawXXX calls: a circle of one color and an ellipse and an arc, set to display a half circle of the same, second color.

Note: To make the angular speed uniform you may want to play with a little Math.Sin and/or an angle table..

If you look at it from any other angle and if you need to show rotating bitmaps in 3D you can't easily draw it but will need to resort to displaying frames..

But you can combine the disk rotation from the link with the code above and will get rather complex rotations, that look a lot like a 3D sphere.. Simply add the code before the drawing..

float bw2 = r2.Width / 2f;    
float bh2 = r2.Height / 2f;  
e.Graphics.TranslateTransform(bw2, bh2);
e.Graphics.RotateTransform(angle / 3);
e.Graphics.TranslateTransform(-bw2, -bh2);

..use the drawing from above instead of the DrawImage line and move the ResetTransform to the end. You will want to use a different or scaled angle!

enter image description hereenter image description hereenter image description hereenter image description here

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
  • i understand, but i dont want to rotate the image within the picturebox, i want the control to rotate it 360 degree clockwise. – Laminores Aug 20 '15 at 10:41
  • That's not possible. A Winforms Control can't rotate. Only the content / the stuff drawn onto it. – TaW Aug 20 '15 at 10:45