I am trying to add rotating of image in my WPF application. There is a code:
public static Bitmap RotateImage(Image inputImage, float angleDegrees)
{
if (angleDegrees == 0f || angleDegrees == 360f)
return (Bitmap)inputImage.Clone();
int oldWidth = inputImage.Width;
int oldHeight = inputImage.Height;
int newWidth = inputImage.Width;
int newHeight = inputImage.Height;
Bitmap newBitmap = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb);
newBitmap.SetResolution(inputImage.HorizontalResolution, inputImage.VerticalResolution);
using (Graphics graphicsObject = Graphics.FromImage(newBitmap))
{
graphicsObject.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsObject.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphicsObject.SmoothingMode = SmoothingMode.HighQuality;
graphicsObject.TranslateTransform(newWidth / 2f, newHeight / 2f);
graphicsObject.RotateTransform(angleDegrees);
graphicsObject.TranslateTransform(-oldWidth / 2f, -oldHeight / 2f);
graphicsObject.DrawImage(inputImage, 0, 0);
}
return newBitmap;
}
Original code: https://stackoverflow.com/a/16027561/11601385
Well, the image is rotated correctly, but there is a problem with image size.
How can I change size of rotated image to make it correct?