Compress size of image to 250kb using xamarin.forms without dependency service

14,990

Solution 1

It is a very complicated job since you would need a ton of knowledge about image processing.

Most importantly, re-inventing wheel is a bad move.

http://www.codeproject.com/Articles/83225/A-Simple-JPEG-Encoder-in-C

Take a look of the above code project which only tackles JPEG; not to say TIFF, GIF, BMP etc.

Image compression involves many complex mathematics transforms, like DCT and Huffman.

You will need a whole university semester to learn those basics.


On the other hand, wisely utilizing platform support, you can complete the task within a minute.

BitmapEncoder in Windows Phone.

FileStream stream = new FileStream("new.jpg", FileMode.Create);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.QualityLevel = 30;
encoder.Frames.Add(BitmapFrame.Create(image));
encoder.Save(stream);

Bitmap in Android

using (System.IO.Stream stream = System.IO.File.Create(targetFile))
{
    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 30, stream);
}

UIImage in iOS

NSData data = image.AsJPEG(0.3);

Bitmap in .NET framework

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
ImageCodecInfo codec = codecs.First(t => t.FormatID == ImageFormat.Jpeg.Guid);
EncoderParameters parameters = new EncoderParameters(1);
parameters.Param[0] = new EncoderParameter(Encoder.Quality, 30L);
bitmap.Save("output.jpg", codec, parameters);

Solution 2

I also had this same problem. Please check here as I believe you will find the solution.

https://xamarincodes.com/2020/04/05/image-compression-in-xamarin-forms/

I used Xam.Plugin.Media – setting the compression quality to take photos and compress as well.

Here is a sample

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);
Share:
14,990
Nawnit
Author by

Nawnit

Around 7.4+ years of experience in software development. Over 2+ years of hands on IT expertise priotizing CI/CD, Automation of configuration management, build/release management, Cloud Resource utilizations, etc. Experience in building native iOS and hybrid mobile application.

Updated on July 22, 2022

Comments

  • Nawnit
    Nawnit almost 2 years

    I'm trying to compress image taken from camera to 250kb size in Xamarin.Forms. I found ways to do that in dependency service but I want it without dependency service (pure xamarin.forms code). How could it possible. Can anyone suggest me best approaches you have?

    Thanks in advance

  • Karan Harsh Wardhan
    Karan Harsh Wardhan about 5 years
    just fyi can't use that .net solution for xamarin since system.drawing is not included in xamarin bugzilla.xamarin.com/show_bug.cgi?id=53385
  • Nitin Sawant
    Nitin Sawant almost 5 years
    can you post xamarin forms version?
  • Ashaar
    Ashaar almost 5 years
    You can use dependency injuction with above paltform specific code