Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement