This project has moved. For the latest updates, please go here.

How can Rotate image without losing the size and alignment

Jul 15, 2015 at 5:37 AM
Hi,

I am first time using the Magick.Net to perform image operations. When I use the Rotate function to rotate the image, it's rotate the image but reduce the image size as well as lost the alignment. Could you please assist how can rotate the image without losing the image size and alignment.
I am using below code to rotate the image.

using (MagickImage image = new MagickImage(imagePath))
{
       image.Rotate(40);
       image.RePage();
}

Thanks,
Gajendra.
Coordinator
Jul 15, 2015 at 6:35 AM
Can you clarify what you mean by alignment? And the size of the image can change depending on the output format. What is your input and what is your output format?
Jul 15, 2015 at 7:39 AM
Edited Jul 15, 2015 at 7:42 AM
When i rotate any image, the image position keep change every time (means If i perform rotation action 2 to 3 times with 50 degree rotation value, the image goes out of the frame.).
And I am using Jpeg images, but i also check with png format and it's also behaving the same.
Please let me know if further information is required.
Coordinator
Jul 16, 2015 at 11:37 AM
Edited Jul 16, 2015 at 11:40 AM
When you rotate an image it will resize the image to make sure the rotated image fits inside the frame. Because you rotate the image multiple times it will become larger and larger. The following demonstrates what is happening:
using (MagickImage image = new MagickImage("logo:"))
{
  image.BackgroundColor = new MagickColor("purple");
  image.Rotate(50);
  image.BackgroundColor = new MagickColor("yellow");
  image.Rotate(50);
  image.Write(@"C:\logo1.png");
}
You will get a better result if you do the whole rotation at once:
using (MagickImage image = new MagickImage("logo:"))
{
  image.BackgroundColor = new MagickColor("purple");
  image.Rotate(100);
  image.Write(@"C:\logo2.png");
}
Or you could trim the image at the end:
using (MagickImage image = new MagickImage("logo:"))
{
  image.BackgroundColor = new MagickColor("purple");
  image.Rotate(50);
  image.Rotate(50);
  image.Trim();
  image.Write(@"C:\logo3.png");
}