title: How to rotate image x degrees in c#?
body: <p>I have done some searching and i can not find any function thats doing what i whant it todo.</p>
  
  <p>I have a image file of a scanned document with text, but the document is some degrees rotated so i whant to rotate it so the text being inline with each other.</p>
  
  <p>In a perfect world its should be a function doing this automaticly but i can not find anything and what i understand to get it to work automaticly its needed to be some analyze of the image and i think its to big thing todo.</p>
  
  <p>But then i have done a tool to rotate the image on a website manually, but now i need a function to save the rotation to the image file.</p>
  
  <p>This seems to be some differents methods for but no one i tested doing what i whant.</p>
  
  <p>The function i have finded that works almost like i whant is:</p>
  
  <pre><code>public static Bitmap RotateImg(Bitmap bmp, float angle, Color bkColor) {
  int w = bmp.Width;
  int h = bmp.Height;
  PixelFormat pf = default(PixelFormat);
  if (bkColor == Color.Transparent)
  {
      pf = PixelFormat.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>But this do not change the height and width of the image file so the image file is in the same size but the image "object" has been scaled and rotated.</p>
  
  <p>Any idea how i can do this good?</p>
  
  <p><strong>Answer</strong>
  I find the solution that worked with my image that has a resolution on 300 at a <a href="https://stackoverflow.com/questions/4320531/rotating-an-image-modifies-the-resolution-and-clarity/4320581#4320581">old answer here</a>.</p>