public RotateTransform ( float angle ) : void | ||
angle | float | |
return | void |
using System.Drawing; Graphics graphics = this.CreateGraphics(); Pen pen = new Pen(Color.Blue, 3); Rectangle rect = new Rectangle(50, 50, 100, 50); graphics.DrawRectangle(pen, rect); Matrix matrix = new Matrix(); matrix.RotateAt(45, new PointF(75, 75)); graphics.Transform = matrix; graphics.DrawRectangle(pen, rect); graphics.Dispose();
using System.Drawing; Image image = Image.FromFile("image.jpg"); Bitmap rotatedImage = new Bitmap(image.Width, image.Height); rotatedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); Graphics graphics = Graphics.FromImage(rotatedImage); graphics.TranslateTransform((float)image.Width / 2, (float)image.Height / 2); graphics.RotateTransform(90); graphics.TranslateTransform(-(float)image.Width / 2, -(float)image.Height / 2); graphics.DrawImage(image, new Point(0, 0)); image.Dispose(); graphics.Dispose();In this example, we are loading an image file using the Image.FromFile method. Then we create a new Bitmap object with the same width and height as the original image. We create a graphics object from this bitmap and use the TranslateTransform, RotateTransform, and DrawImage methods to rotate the image by 90 degrees around its center point. Finally, we dispose of the original image and graphics objects.