0

I have seen a couple of examples, but nothing helped me so far with this issue.

I have an image in byte[] which size must be reduced to under 2 MB. I tried a couple of things, but nothing helped so far.

I used the following code which could be found at many questions like this:

public static byte[] Compress(byte[] data)
        {
            MemoryStream output = new MemoryStream();
            using (DeflateStream dstream = new DeflateStream(output, CompressionLevel.Optimal))
            {
                dstream.Write(data, 0, data.Length);
            }
            return output.ToArray();
        }

This does reduce the size of the image, but not below 2 MB and I have no idea how to state that here. Other examples were focused on saving the image on the phone with reduced size, but my image should remain in byte[]. This question thus does not help.

I hope someone can help.

Ganesh Gebhard
  • 453
  • 1
  • 7
  • 20
  • 1
    You can compress a bitmap image by saving it with a higher compression ratio and/or resizing the dimensions. Just compressing the byte[] isn't going to be very successful. After you compress the image then you can transform it back into a byte[]. There are **many** [existing questions](https://www.google.com/search?q=xamarin+compress+image+site:stackoverflow.com) on this topic – Jason Feb 03 '23 at 01:33
  • I hope to find a solution where I don't need to save the image. – Ganesh Gebhard Feb 03 '23 at 02:13
  • 1
    When I said save I didn’t mean you have to save it to disk. Many image libraries will allow you to operate in memory – Jason Feb 03 '23 at 02:15
  • Do you have an example of this? I use this as a CameraView: https://learn.microsoft.com/en-us/xamarin/community-toolkit/views/cameraview . I was trying to find a way in MediaCaptured, but I found nothing. – Ganesh Gebhard Feb 03 '23 at 02:22
  • 1
    I just gave you a link to dozens of related questions – Jason Feb 03 '23 at 02:30

1 Answers1

0

You can try to use nuget Xam.Plugin.Media to set the compression quality to take photos and compress as well.

Please refer to the following code:

private async void cmdCameraPhotograph_Clicked(object sender, EventArgs e)
    {
        if (CrossMedia.Current.IsTakePhotoSupported)
        {
            var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            {
                Directory = "Photographs",
                SaveToAlbum = true,
                CompressionQuality = 40,
                CustomPhotoSize = 35,
                PhotoSize = PhotoSize.MaxWidthHeight,
                MaxWidthHeight = 2000,
                DefaultCamera = CameraDevice.Rear
            }).ConfigureAwait(true);

            if (file != null)
            {
                
            }
        }
        else
        {
            await DisplayAlert("Not Supported", "Your device does not support this feature.", "OK. Understood")
                .ConfigureAwait(true);
        }
    }

You can also get the file from the Gallery

 var file = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions
        {
            CompressionQuality = 40,
            CustomPhotoSize = 35,
            PhotoSize = PhotoSize.MaxWidthHeight,
            MaxWidthHeight = 2000
        }).ConfigureAwait(true);

You can also compress images on different platforms individually. For more information,you can check thread Compress images.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19