The following code will rotate an image by 45 degrees and
enlarge it a bit. To make sure the image is fully visible we also
need to translate it so that all the corners are visible. To
determine this we transform the corners and convert them into a
bounds rectangle. Then we can translate the image so that it
appears at the origin and we can resize the Bitmap so that it is is
the right size.
[C#]
using (Image image = Bitmap.FromFile(Server.MapPath("rez/felix-tchverkin-7pkN83wDZwY-unsplash.jpg"))) {
using (Bitmap bm = new Bitmap(new Size())) {
using (Graphics graphics = Graphics.FromImage(bm)) {
graphics.RotateTransform(45);
graphics.ScaleTransform(1.3, 1.3);
PointF[] corners = ((RectangleF)image.Bounds).Corners;
graphics.Transform.TransformPoints(corners);
Rectangle bounds = (Rectangle)RectangleF.FromPoints(corners);
graphics.TranslateTransform(-bounds.Left, -bounds.Top, MatrixOrder.Append);
graphics.DrawImage(image, image.Bounds);
bm.Resize(bounds.Width, bounds.Height);
}
bm.Save(Server.MapPath($"IG8_Graphics_RotateTransform.jpg"));
}
}
felix-tchverkin-7pkN83wDZwY-unsplash.jpg
IG8_Graphics_RotateTransform.jpg
|