rotate image on iphone in Xamarin can't tell if the camera is portrait or landscape

rick ruhl 61 Reputation points
2023-06-01T23:56:04.3566667+00:00

when I call this after calling, it will turn the bitmap 90 degrees in portrait, but will also in landscape, where I dont want it to be turned 90 degrees.

This is to work about a bug in

MediaPicker.CapturePhotoAsync();
 var photo = await MediaPicker.CapturePhotoAsync();


 Stream stream = await photo.OpenReadAsync();

               

                SKBitmap photoBitmap = RotateBitmap(SKBitmap.Decode(stream));
              

 SKBitmap RotateBitmap(SKBitmap bitmap)
        {
            var mainDisplayInfo = DeviceDisplay.MainDisplayInfo;
            var orientation = mainDisplayInfo.Orientation;

            var currentOrientation = UIApplication.SharedApplication.StatusBarOrientation;


                SKSizeI sk = new SKSizeI(400, 400);
            SKBitmap rotatedBitmap = new SKBitmap(bitmap.Height, bitmap.Width);

                 using (var surface = new SKCanvas(rotatedBitmap))
                {
                 surface.Translate(rotatedBitmap.Width, 0);
                
            if (currentOrientation == UIInterfaceOrientation.Portrait)
                {
                        surface.RotateDegrees(90);
                        }
                    else
                    surface.RotateDegrees(0);
                surface.DrawBitmap(bitmap, 0, 0);
                }
                // Orientation got changed! Do your changes here
           
           
           
           /// var dstInfo = new SKImageInfo(200, 300);
         //   rotatedBitmap.Resize(dstInfo, SKFilterQuality.High);
            return rotatedBitmap;
        }
Developer technologies | .NET | Xamarin
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.