123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- using System;
- using System.Drawing;
- using System.IO;
- using Android.Graphics;
- using Bitmap = Android.Graphics.Bitmap;
- [assembly: Xamarin.Forms.Dependency(typeof(InABox.Mobile.Android.ImageToolsDroid))]
- namespace InABox.Mobile.Android
- {
- public class ImageToolsDroid: IImageTools
- {
- public byte[] CreateThumbnail(byte[] source, float maxwidth, float maxheight)
- {
- byte[] result = { };
- using (var image = BitmapFactory.DecodeByteArray(source, 0, source.Length))
- {
- if (image != null)
- {
- var size = new Size((int)image.GetBitmapInfo().Height, (int)image.GetBitmapInfo().Width);
- if ((size.Width > 0) && (size.Height > 0))
- {
- var maxFactor = Math.Min(maxwidth / size.Width, maxheight / size.Height);
- var width = maxFactor * size.Width;
- var height = maxFactor * size.Height;
- using (var tgt = Bitmap.CreateScaledBitmap(image, (int)height, (int)width, true))
- {
- if (tgt != null)
- {
- using (var ms = new MemoryStream())
- {
- tgt.Compress(Bitmap.CompressFormat.Jpeg, 95, ms);
- result = ms.ToArray();
- }
- tgt.Recycle();
- }
- }
- }
- image.Recycle();
- }
- }
- return result;
- }
- }
- }
|