title: How can I rotate an image by any degree?
body: <p>I have animated gif and I'm using a class to parse the images(frames) from it.
  The class is:</p>
  <pre><code>using System;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.Collections.Generic;
  using System.IO;
  
  public class AnimatedGif
  {
      private List&lt;AnimatedGifFrame&gt; mImages = new List&lt;AnimatedGifFrame&gt;();
      public AnimatedGif(string path)
      {
          Image img = Image.FromFile(path);
          int frames = img.GetFrameCount(FrameDimension.Time);
          if (frames &lt;= 1) throw new ArgumentException(&quot;Image not animated&quot;);
          byte[] times = img.GetPropertyItem(0x5100).Value;
          int frame = 0;
          for (; ; )
          {
              int dur = BitConverter.ToInt32(times, 4 * frame);
              mImages.Add(new AnimatedGifFrame(new Bitmap(img), dur));
              if (++frame &gt;= frames) break;
              img.SelectActiveFrame(FrameDimension.Time, frame);
          }
          img.Dispose();
      }
      public List&lt;AnimatedGifFrame&gt; Images { get { return mImages; } }
  }
  
  public class AnimatedGifFrame
  {
      private int mDuration;
      private Image mImage;
      internal AnimatedGifFrame(Image img, int duration)
      {
          mImage = img; mDuration = duration;
      }
      public Image Image { get { return mImage; } }
      public int Duration { get { return mDuration; } }
  }
  </code></pre>
  <p>Now in form1 I loop over the frames in this case 4 and I want to rotate the animation by any degree. Now its rotating each 45 or 90 degrees. I want to add more frames(images) to the animation so if I set the rotation to 31 or to 10 degrees so I will see the animation rotating in 10 degrees.</p>
  <p>This is the code in Form1 which is not working good. I'm using a function for the rotation which I didn't test yet if its any working.</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  namespace AnimatedGifEditor
  {
      public partial class Form1 : Form
      {
          Image myImage;
          AnimatedGif myGif;
          Bitmap bitmap;
  
          public Form1()
          {
              InitializeComponent();
  
              myImage = Image.FromFile(@&quot;D:\fananimation.gif&quot;);
              myGif = new AnimatedGif(@&quot;D:\fananimation.gif&quot;);
              for (int i = 0; i &lt; myGif.Images.Count; i++)
              {
                  pictureBox1.Image = myGif.Images[3].Image;
                  bitmap = new Bitmap(pictureBox1.Image);
                  rotateImage(bitmap, 76);
                  pictureBox1.Image = bitmap;
              }
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
  
          }
  
  private Bitmap RotateImg(Bitmap bmp, float angle, Color bkColor)
      {
          int w = bmp.Width;
          int h = bmp.Height;
          bmp.PixelFormat pf = default(bmp.PixelFormat);
          if (bkColor == Color.Transparent)
          {
              pf = bmp.Format32bppArgb;
          }
          else
          {
              pf = bmp.PixelFormat;
          }
  
          Bitmap tempImg = new Bitmap(w, h, pf);
          Graphics g = Graphics.FromImage(tempImg);
          g.Clear(bkColor);
          g.DrawImageUnscaled(bmp, 1, 1);
          g.Dispose();
  
          GraphicsPath path = new GraphicsPath();
          path.AddRectangle(new RectangleF(0f, 0f, w, h));
          Matrix mtrx = new Matrix();
          //Using System.Drawing.Drawing2D.Matrix class 
          mtrx.Rotate(angle);
          RectangleF rct = path.GetBounds(mtrx);
          Bitmap newImg = new Bitmap(Convert.ToInt32(rct.Width), Convert.ToInt32(rct.Height), pf);
          g = Graphics.FromImage(newImg);
          g.Clear(bkColor);
          g.TranslateTransform(-rct.X, -rct.Y);
          g.RotateTransform(angle);
          g.InterpolationMode = InterpolationMode.HighQualityBilinear;
          g.DrawImageUnscaled(tempImg, 0, 0);
          g.Dispose();
          tempImg.Dispose();
          return newImg;
      }
      }
  }
  </code></pre>
  <p>The animated gif I'm using for the test can be found here:</p>
  <p><a href="https://i.stack.imgur.com/Atj8j.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Atj8j.gif" alt="enter image description here" /></a></p>