Advertisement
Yassine_Abbani

Coding

Sep 6th, 2021 (edited)
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4.  
  5. namespace RotatePictureBox
  6. {
  7.     /// <summary>
  8.     /// Summary description for Utilities.
  9.     /// </summary>
  10.     public sealed class Utilities
  11.     {
  12.         private Utilities()
  13.         {
  14.         }
  15.         /// <summary>
  16.         /// Creates a new Image containing the same image only rotated
  17.         /// </summary>
  18.         /// <param name="image">The <see cref="System.Drawing.Image"/> to rotate</param>
  19.         /// <param name="angle">The amount to rotate the image, clockwise, in degrees</param>
  20.         /// <returns>A new <see cref="System.Drawing.Bitmap"/> of the same size rotated.</returns>
  21.         /// <exception cref="System.ArgumentNullException">Thrown if <see cref="image"/> is null.</exception>
  22.         public static Bitmap RotateImage(Image image, float angle)
  23.         {
  24.             return RotateImage(image, new PointF((float)image.Width / 2, (float)image.Height / 2), angle);
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Creates a new Image containing the same image only rotated
  29.         /// </summary>
  30.         /// <param name="image">The <see cref="System.Drawing.Image"/> to rotate</param>
  31.         /// <param name="offset">The position to rotate from.</param>
  32.         /// <param name="angle">The amount to rotate the image, clockwise, in degrees</param>
  33.         /// <returns>A new <see cref="System.Drawing.Bitmap"/> of the same size rotated.</returns>
  34.         /// <exception cref="System.ArgumentNullException">Thrown if <see cref="image"/> is null.</exception>
  35.         public static Bitmap RotateImage(Image image, PointF offset, float angle)
  36.         {
  37.             if (image == null)
  38.                 throw new ArgumentNullException("image");
  39.  
  40.             //create a new empty bitmap to hold rotated image
  41.             Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
  42.             rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
  43.  
  44.             //make a graphics object from the empty bitmap
  45.             Graphics g = Graphics.FromImage(rotatedBmp);
  46.  
  47.             //Put the rotation point in the center of the image
  48.             g.TranslateTransform(offset.X, offset.Y);
  49.  
  50.             //rotate the image
  51.             g.RotateTransform(angle);
  52.  
  53.             //move the image back
  54.             g.TranslateTransform(-offset.X, -offset.Y);
  55.  
  56.             //draw passed in image onto graphics object
  57.             g.DrawImage(image, new PointF(0, 0));
  58.  
  59.             return rotatedBmp;
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement