question

MdZakirHossain-8307 avatar image
0 Votes"
MdZakirHossain-8307 asked ·

Rotate Image in Win2D

I'm trying to rotate image but couldn't get expected result. I've tried with WIn2D but couldn't make image as expected.

 public async void Rotate(string originalImagepath, Rect originalImageRect, float degrees)
     {
         int height = (int)Math.Sqrt(originalImageRect.Width * originalImageRect.Width + originalImageRect.Height * originalImageRect.Height);
    
         CanvasDevice device = CanvasDevice.GetSharedDevice();
         CanvasRenderTarget webCardImage = null;
         CanvasBitmap bitmap = null;
         var logicalDpi = DisplayInformation.GetForCurrentView().LogicalDpi;
         Vector2 endpoint = new Vector2((float)originalImageRect.Width / 2, (float)originalImageRect.Height / 2);
    
         try
         {
             webCardImage = new CanvasRenderTarget(device, height, height, logicalDpi);
             using (var ds = webCardImage.CreateDrawingSession())
             {
                 ds.Clear(Colors.Transparent);
    
                 using (FileStream imageStream = new FileStream(originalImagepath, FileMode.Open))
                 {
                     IRandomAccessStream fileStream = imageStream.AsRandomAccessStream();
                     bitmap = await CanvasBitmap.LoadAsync(device, fileStream);
                 }
    
                 ICanvasImage image = new Transform2DEffect
                 {
                     Source = bitmap,
                     TransformMatrix = Matrix3x2.CreateRotation(degrees, endpoint),
                 };
                 var sourceRect = image.GetBounds(ds);
                 ds.DrawImage(image, new Rect(originalImageRect.X, originalImageRect.Y, originalImageRect.Width, originalImageRect.Height), sourceRect, 1, CanvasImageInterpolation.HighQualityCubic);
    
             }
         }
         catch (Exception ex)
         {
    
         }
         //Save Image Code here
     }

I want rotate image as 30 degree. After executing my code i got unexpected result.

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

jtorjo avatar image
0 Votes"
jtorjo answered ·

I believe your code is correct. However, you need to convert your degrees to radian degrees:

 private static double radian(double degrees) {
     return degrees * Math.PI / 180d;
 }

This should fix it.

· 3 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks a lot. it works

0 Votes 0 ·
jtorjo avatar image jtorjo MdZakirHossain-8307 ·

Awesome, glad to have helped!

0 Votes 0 ·
wfrf92 avatar image wfrf92 MdZakirHossain-8307 ·

Hiii did you use crop also ? could you please share me the code if you have thanks

0 Votes 0 ·