0

How do you roate an image inside of a PictureBox? I have a simple pacman.gif image in my PictureBox and it works fine at runtime (moving in all directions). But I want to rotate my gif as I press Left or Right key. Actually it rotates fine but at runtime the animation stops working.

 private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Left:
                pictureBox1.Left -= 7;
                break;
            case Keys.Right:
                  pictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX);
        pictureBox1.Left += 7;
                break;
            case Keys.Up:
                pictureBox1.Top -= 7;
                break;
            case Keys.Down:
                pictureBox1.Top += 7;
                break;
        }

    }

Here's My Code. But gif isn't working.

zubaida12
  • 53
  • 5
  • Nothing in your code would rotate the image. So, what do you mean by "it rotates fine"? Please post the code that is supposed to do the rotation, if you have any. – vesan Feb 15 '16 at 21:52
  • 2
    Possible duplicate of http://stackoverflow.com/questions/2163829/how-do-i-rotate-a-picture-in-c-sharp unless the issue has to do with the keypresses. Not enough information. – Khale_Kitha Feb 15 '16 at 21:52
  • 1
    ... or my version [Using a matrix to rotate rectangles individually](http://stackoverflow.com/a/10210639/719186) – LarsTech Feb 15 '16 at 21:54
  • This SO answer may help as well http://stackoverflow.com/a/4144124/1726499 – Teis Lindemark Feb 15 '16 at 22:19

1 Answers1

2

You are on the right track using the RotateFlip method at least in principle, although I don't see you doing a rotation, only a flip in the code you show.

Don't listen to folks who link to free rotations code; you only need to rotate by 90° or multiples..

However you just can't rotate an animated gif and expect it to work afterwards.

Instead you should use a gif creation/editing tool and create the rotated and/or flipped versions there and load the right one when doing a turn.

The GDI methods will all only rotate the 1st frame and effectively destroy the the animation in the gif. Manipulating animated gifs in c# ist not really supported.

TaW
  • 53,122
  • 8
  • 69
  • 111
  • 1
    No problem; it is just a pity that I didn't have better news than that. If you are happy with the answer, please consider [accepting](http://stackoverflow.com/help/accepted-answer) it.. – TaW Feb 16 '16 at 09:42
  • 1
    You can rotate an animated gif, but not easily without external help. For example, using ImageMagick, you can do it (see: http://stackoverflow.com/a/28073625/390421). There's a dotNet wrapper for ImageMagick that exposes some of the functionality. ImageMagick rocks. :) – MartijnK Feb 16 '16 at 09:56
  • Good to know! I would stll suggest to create the variations in advance and switch when doing a turn. – TaW Feb 16 '16 at 09:59