How can I rotate an image automatically from OnActivityResult?

Joao Carlos Del Vecchio 41 Reputation points
2022-10-21T15:48:41.65+00:00

How can I rotate the image that comes to me from OnActivityResult and then transform it into byte [] to reduce it?
I start the image selection like this:

Intent = new Intent();  
            Intent.SetType("image/*");  
            Intent.SetAction(Intent.ActionGetContent);  
            StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), PickImageId);  

This is what I have in OnActivityResult:

if ((requestCode == PickImageId) && (resultCode == Result.Ok))  
                    {  
                        Android.Net.Uri uri = data.Data;  
                        ImmagineProfilo.SetImageURI(uri);  
                        ...  
                        bitmapData = ResizeImageAndroid(bitmapData, 800);  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,335 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,827 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,316 Reputation points Microsoft Vendor
    2022-10-24T08:42:27.17+00:00

    Hello,

    after the image rotation I should turn it into byte []

    You can use MemoryStream to convert your bitmap to byte[] like following code.

       using (var stream = new MemoryStream())  
                   {  
                       rotatedBitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 0, stream);  
                       byte[] bitmapData = stream.ToArray();  
                   }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Joao Carlos Del Vecchio 41 Reputation points
    2022-10-25T10:30:21.697+00:00

    It might be useful to someone.
    Just choose which size to take into account.

    //  
     public byte[] ImmagineRiduciRuota(Android.Net.Uri ImageUri, int width=0, int height=0)  
                {  
                      
                    var IMGStream = ContentResolver.OpenInputStream(ImageUri);  
                    var originalImage = BitmapFactory.DecodeStream(IMGStream);  
                    // Load the bitmap  
                    int Altezza = 0;  
                    int Larghezza = 0;  
                    if (width != 0)  
                    {  
                        if (height == 0)  
                        {  
                            var PercentualeScalaH = (width * 100) / originalImage.Width;  
                            Altezza = (int)(originalImage.Height * PercentualeScalaH) / 100;  
                        }  
                    }  
                    else  
                    {  
                        if(height !=0)  
                        {  
                            var PercentualeScalaW = (height * 100) / originalImage.Height;  
                            Larghezza = (int)(originalImage.Width * PercentualeScalaW) / 100;  
                        }  
                    }  
                    Altezza = Altezza == 0 ? height : Altezza;  
                    Larghezza = Larghezza == 0 ? width : Larghezza;  
                    Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)Larghezza, (int)Altezza, false);  
          
                    using MemoryStream ms = new MemoryStream();  
                    //  
                    var IMGStream2 = ContentResolver.OpenInputStream(ImageUri);  
          
                    ExifInterface exif = new ExifInterface(IMGStream2);  
                    var matrix = new Matrix();  
                    var orientation = (Orientation)exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Undefined);  
                    Log.Debug(TAG, "orientation='" + orientation.ToString() + "'");  
                    switch (orientation)  
                    {  
                        case Orientation.Normal:  
                            // landscape. Non fare nulla di   
                            break;  
                        case Orientation.Rotate180:  
                            matrix.PreRotate(180);  
                            break;  
                        case Orientation.Rotate270:  
                            matrix.PreRotate(270);  
                            break;  
                        case Orientation.Rotate90:  
                            matrix.PreRotate(90);  
                            break;  
                        default:  
                            // gestisce il ritratto e altri orientamenti che potremmo non aver bisogno di gestire.  
                            matrix.PreRotate(90);  
                            break;  
                    }  
          
                    resizedImage = Bitmap.CreateBitmap(resizedImage, 0, 0, resizedImage.Width, resizedImage.Height, matrix, false);  
                    matrix.Dispose();  
                    matrix = null;  
                    resizedImage.Compress(Bitmap.CompressFormat.Jpeg, 80, ms);  
          
                    int quality = 100;  
                    using (var stream2 = new System.IO.MemoryStream())  
                    {  
                        resizedImage.Compress(Bitmap.CompressFormat.Jpeg, quality, stream2);  
                        resizedImage.Recycle();  
                        resizedImage.Dispose();  
                        resizedImage = null;  
                        return stream2.ToArray();  
                    }  
                    //  
                }  
      
    
    0 comments No comments

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.