question

VasanthakumarM-3635 avatar image
0 Votes"
VasanthakumarM-3635 asked JarvanZhang-MSFT commented

Compress image while uploading

Hi Xperts,


How to compress the image while uploading the xamarin forms.


dotnet-xamarin
5 |1600 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

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered JarvanZhang-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

How to compress the image while uploading the xamarin forms.

Try to create an 'ImageResizer' control class to resize the image on each platform. Check the code:

public static class ImageResizer
{
    static ImageResizer()
    {
    }

    public static async Task<byte[]> ResizeImage(byte[] imageData, float width, float height)
    {
        var platform = Device.RuntimePlatform;
        switch (platform)
        {
            case Device.Android:
                return ResizeImageAndroid(imageData, width, height);
            case Device.iOS:
                return ResizeImageIOS(imageData, width, height);
            default:
                break;
        }

        return null;
    }
    public static byte[] ResizeImageAndroid(byte[] imageData, float width, float height)
    {
        // Load the bitmap
        Bitmap originalImage = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
        Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)width, (int)height, false);

        using (MemoryStream ms = new MemoryStream())
        {
            resizedImage.Compress(Bitmap.CompressFormat.Jpeg, 100, ms);
            return ms.ToArray();
        }
    }
    public static byte[] ResizeImageIOS(byte[] imageData, float width, float height)
    {
        UIImage originalImage = ImageFromByteArray(imageData);
        UIImageOrientation orientation = originalImage.Orientation;

        //create a 24bit RGB image
        using (CoreGraphics.CGBitmapContext context = new CoreGraphics.CGBitmapContext(IntPtr.Zero,
                                             (int)width, (int)height, 8,
                                             4 * (int)width, CoreGraphics.CGColorSpace.CreateDeviceRGB(),
                                             CoreGraphics.CGImageAlphaInfo.PremultipliedFirst))
        {

            RectangleF imageRect = new RectangleF(0, 0, width, height);

            // draw the image
            context.DrawImage(imageRect, originalImage.CGImage);

            UIKit.UIImage resizedImage = UIKit.UIImage.FromImage(context.ToImage(), 0, orientation);

            // save the image as a jpeg
            return resizedImage.AsJPEG().ToArray();
        }
    }

    public static UIKit.UIImage ImageFromByteArray(byte[] data)
    {
        if (data == null)
        {
            return null;
        }

        UIKit.UIImage image;
        try
        {
            image = new UIKit.UIImage(Foundation.NSData.FromArray(data));
        }
        catch (Exception e)
        {
            Console.WriteLine("Image load failed: " + e.Message);
            return null;
        }
        return image;
    }
}

Here is the related sample: https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/xamformsimageresize/

Best Regards,

Jarvan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.

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.


· 2
5 |1600 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.

@JarvanZhang-MSFT Do you have any idea about cropping the image while uploading. For your information I used Xam.Plugin.media.please help me this

0 Votes 0 ·

Do you have any idea about cropping the image while uploading.

Hi, VasanthakumarM. Please ask one question at a time, please open a new thread for the another problem. So that the members could help you better.

For your information I used Xam.Plugin.media.please help me this

Please get the byte array from the picked file, then compress the bytes.

0 Votes 0 ·