简体   繁体   中英

How to Compress Image but not resize it Using Xamarin Android?

I am getting a image from a server which is pretty large around 15MB but i want to keep the Aspect Ratio but compress the size of the file because I am loading multiple files that around the same size? these Image are downloaded as BitMaps and Used SetImageBitmap to display the image

You can do this by converting the image to a jpeg or png. This is quick and dirty implementation of a Bitmap to PNG conversion routine:

public string ResizeImage(string sourceFilePath)
{
    Android.Graphics.Bitmap bmp = Android.Graphics.BitmapFactory.DecodeFile (sourceFilePath);

    string newPath = sourceFilePath.Replace(".bmp", ".png");
    using (var fs = new FileStream (newPath, FileMode.OpenOrCreate)) {
        bmp.Compress (Android.Graphics.Bitmap.CompressFormat.Png, 100, fs);
    }

    return newPath;
}

It makes assumptions on the file extension but that can modified fairly easily.

Here is the full sample I used to verify the compression:

public class MainActivity : Activity
{
    public const string BITMAP_URL = @"http://www.openjpeg.org/samples/Bretagne2.bmp";


    public string ResizeImage(string sourceFilePath)
    {
        Android.Graphics.Bitmap bmp = Android.Graphics.BitmapFactory.DecodeFile (sourceFilePath);

        string newPath = sourceFilePath.Replace(".bmp", ".png");
        using (var fs = new FileStream (newPath, FileMode.OpenOrCreate)) {
            bmp.Compress (Android.Graphics.Bitmap.CompressFormat.Png, 100, fs);
        }

        return newPath;
    }

    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.Main);

        Button button = FindViewById<Button> (Resource.Id.myButton);

        button.Click += delegate {
            System.Threading.Tasks.Task.Run( () => {
                RunOnUiThread( () => Toast.MakeText(this, "Downloading file", ToastLength.Long).Show());

                string downloadFile = DownloadSourceImage(BITMAP_URL);

                RunOnUiThread( () => Toast.MakeText(this, "Rescaling image: " + downloadFile, ToastLength.Long).Show());

                string convertedFile = ResizeImage(downloadFile);

                var bmpFileSize = (new FileInfo(downloadFile)).Length;
                var pngFileSize = (new FileInfo(convertedFile)).Length;

                RunOnUiThread( () => Toast.MakeText(this, "BMP is " + bmpFileSize + "B. PNG is " + pngFileSize + "B.", ToastLength.Long).Show());
            });
        };
    }

    public string DownloadSourceImage(string url)
    {
        System.Net.WebClient client = new System.Net.WebClient ();

        string fileName = url.Split ('/').LastOrDefault ();
        string downloadedFilePath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, fileName);

        if (File.Exists (downloadedFilePath) == false) {
            client.DownloadFile (url, downloadedFilePath);
        }

        return downloadedFilePath;
    }
}

You can compress the image using other file formats (eg. jpeg). Or you can resize the image while maintaining the aspect ratio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM