Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Go to top

C# Image/PictureBox Rotations

, 16 Feb 2010
How to rotate any image from the center of that image
RotatePictureBox.zip
RotatePictureBox.exe
RotatePictureBoxSource.zip
RotatePictureBox
RotatePictureBox
bin
Debug
Release
obj
Debug
Refactor
RotatePictureBox.csproj.GenerateResource.Cache
RotatePictureBox.exe
RotatePictureBox.Form1.resources
RotatePictureBox.pdb
RotatePictureBox.Properties.Resources.resources
TempPE
Release
RotatePictureBox.csproj.GenerateResource.Cache
RotatePictureBox.exe
RotatePictureBox.Form1.resources
RotatePictureBox.pdb
RotatePictureBox.Properties.Resources.resources
TempPE
Properties
Settings.settings
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace RotatePictureBox
{
    /// <summary>
    /// Summary description for Utilities.
    /// </summary>
    public sealed class Utilities
    {
        private Utilities()
        {
        }
        /// <summary>
        /// Creates a new Image containing the same image only rotated
        /// </summary>
        /// <param name="image">The <see cref="System.Drawing.Image"/> to rotate</param>
        /// <param name="angle">The amount to rotate the image, clockwise, in degrees</param>
        /// <returns>A new <see cref="System.Drawing.Bitmap"/> of the same size rotated.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <see cref="image"/> is null.</exception>
        public static Bitmap RotateImage(Image image, float angle)
        {
            return RotateImage(image, new PointF((float)image.Width / 2, (float)image.Height / 2), angle);
        }

        /// <summary>
        /// Creates a new Image containing the same image only rotated
        /// </summary>
        /// <param name="image">The <see cref="System.Drawing.Image"/> to rotate</param>
        /// <param name="offset">The position to rotate from.</param>
        /// <param name="angle">The amount to rotate the image, clockwise, in degrees</param>
        /// <returns>A new <see cref="System.Drawing.Bitmap"/> of the same size rotated.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if <see cref="image"/> is null.</exception>
        public static Bitmap RotateImage(Image image, PointF offset, float angle)
        {
            if (image == null)
                throw new ArgumentNullException("image");

            //create a new empty bitmap to hold rotated image
            Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
            rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);

            //make a graphics object from the empty bitmap
            Graphics g = Graphics.FromImage(rotatedBmp);

            //Put the rotation point in the center of the image
            g.TranslateTransform(offset.X, offset.Y);

            //rotate the image
            g.RotateTransform(angle);

            //move the image back
            g.TranslateTransform(-offset.X, -offset.Y);

            //draw passed in image onto graphics object
            g.DrawImage(image, new PointF(0, 0));

            return rotatedBmp;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

MusicMonkey5555

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 16 Feb 2010
Article Copyright 2010 by MusicMonkey5555
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid