Forum Xamarin.Android

An image processing library, that can resize an image, and get it's dimensions

As the title says. I am looking for an image processing library that can resize and image, and get the image dimensions.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai
    edited February 19

    In the xamarin android, you do not need library to resize the image, just wirte a method, set the width and height by yourself.

    public byte[] ResizeImage(byte[] imageData, float width, float height)
        {
            // Load the bitmap 
            BitmapFactory.Options options = new BitmapFactory.Options();// Create object of bitmapfactory's option method for further option use
            options.InPurgeable = true; // inPurgeable is used to free up memory while required
            Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length, options);
    
            float newHeight = 0;
            float newWidth = 0;
    
            var originalHeight = originalImage.Height;
            var originalWidth = originalImage.Width;
    
            if (originalHeight > originalWidth)
            {
                newHeight = height;
                float ratio = originalHeight / height;
                newWidth = originalWidth / ratio;
            }
            else 
            {
                newWidth = width;
                float ratio = originalWidth / width;
                newHeight = originalHeight / ratio;
            }
    
            Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)newWidth, (int)newHeight, true);
    
            originalImage.Recycle();
    
            using (MemoryStream ms = new MemoryStream())
            {
                resizedImage.Compress(Bitmap.CompressFormat.Png, 100, ms);
    
                resizedImage.Recycle();
    
                return ms.ToArray();
            }
    

    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

  • yjh0000yjh0000 Member
    edited February 21

    @LeonLu thank you for your answer. But I do not have a way to get the image width or height. As stated in the question "...and get the image dimensions."

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    I do not have a way to get the image width or height

    You can set it by yourself when call this method.

Sign In or Register to comment.