How do you turn the image in the file?



  • A lot of people must have met with a small but widespread problem, when the image of the stored in the graphic file has an uncomfortable orientation, and it opens search programmes at the wrong angle the user would like to see it. Fortunately, even the most primitive image screening programmes often give the user the opportunity to turn the image, at least on the corners of 90°. Some programmes, including even built-in casings of modern cameras, can even predict which angle the wrongly designed image file needs to be turned, and display the user at the right angle.

    What means of C# and platform .Net can an opportunity be realized to change the image file by turning the second to a given angle and keeping this change in the first?



  • /// <summary>
    /// поворачивает изображение по часовой стрелке или против часовой стрелки
    /// </summary>
    /// <param name="img">изображение</param>
    /// <param name="rotationAngle">угол (в градусах).
    /// Положительные числа - по часовой стрелке
    /// отрицательные - против часовой стрелки
    /// </param>
    /// <returns></returns>
    public static Image RotateImage(Image img, float rotationAngle)
    {
        Bitmap bmp = new Bitmap(img.Width, img.Height);
    
        Graphics gfx = Graphics.FromImage(bmp);
    
        gfx.TranslateTransform((float)bmp.Width / 2, (float)bmp.Height / 2);
    
        gfx.RotateTransform(rotationAngle);
    
        gfx.TranslateTransform(-(float)bmp.Width / 2, -(float)bmp.Height / 2);
    
        gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
        gfx.DrawImage(img, new Point(0, 0));
    
        gfx.Dispose();
    
        return bmp;
    }
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2