It might be useful to someone.
Just choose which size to take into account.
//
public byte[] ImmagineRiduciRuota(Android.Net.Uri ImageUri, int width=0, int height=0)
{
var IMGStream = ContentResolver.OpenInputStream(ImageUri);
var originalImage = BitmapFactory.DecodeStream(IMGStream);
// Load the bitmap
int Altezza = 0;
int Larghezza = 0;
if (width != 0)
{
if (height == 0)
{
var PercentualeScalaH = (width * 100) / originalImage.Width;
Altezza = (int)(originalImage.Height * PercentualeScalaH) / 100;
}
}
else
{
if(height !=0)
{
var PercentualeScalaW = (height * 100) / originalImage.Height;
Larghezza = (int)(originalImage.Width * PercentualeScalaW) / 100;
}
}
Altezza = Altezza == 0 ? height : Altezza;
Larghezza = Larghezza == 0 ? width : Larghezza;
Bitmap resizedImage = Bitmap.CreateScaledBitmap(originalImage, (int)Larghezza, (int)Altezza, false);
using MemoryStream ms = new MemoryStream();
//
var IMGStream2 = ContentResolver.OpenInputStream(ImageUri);
ExifInterface exif = new ExifInterface(IMGStream2);
var matrix = new Matrix();
var orientation = (Orientation)exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Undefined);
Log.Debug(TAG, "orientation='" + orientation.ToString() + "'");
switch (orientation)
{
case Orientation.Normal:
// landscape. Non fare nulla di
break;
case Orientation.Rotate180:
matrix.PreRotate(180);
break;
case Orientation.Rotate270:
matrix.PreRotate(270);
break;
case Orientation.Rotate90:
matrix.PreRotate(90);
break;
default:
// gestisce il ritratto e altri orientamenti che potremmo non aver bisogno di gestire.
matrix.PreRotate(90);
break;
}
resizedImage = Bitmap.CreateBitmap(resizedImage, 0, 0, resizedImage.Width, resizedImage.Height, matrix, false);
matrix.Dispose();
matrix = null;
resizedImage.Compress(Bitmap.CompressFormat.Jpeg, 80, ms);
int quality = 100;
using (var stream2 = new System.IO.MemoryStream())
{
resizedImage.Compress(Bitmap.CompressFormat.Jpeg, quality, stream2);
resizedImage.Recycle();
resizedImage.Dispose();
resizedImage = null;
return stream2.ToArray();
}
//
}