public async Task <Stream> PickImage(ImageLocation location = ImageLocation.All, int maxSize = int.MaxValue) { var raw = await PickImageRaw(location); if (raw == null) { return(null); } using (var stream = await raw.OpenReadAsync()) { var decoder = await BitmapDecoder.CreateAsync(stream); var size = MediaHelper.Crop((int)decoder.PixelWidth, (int)decoder.PixelHeight, maxSize); if (size.Item1 != decoder.PixelWidth || size.Item2 != decoder.PixelHeight) { var ms = new InMemoryRandomAccessStream(); var encoder = await BitmapEncoder.CreateForTranscodingAsync(ms, decoder); encoder.BitmapTransform.ScaledWidth = (uint)size.Item1; encoder.BitmapTransform.ScaledHeight = (uint)size.Item2; await encoder.FlushAsync(); return(ms.AsStreamForRead()); } } return(await raw.OpenStreamForReadAsync()); }
private static UIImage ResizeImage(UIImage image, int maxSize) { var newSize = MediaHelper.Crop((int)image.Size.Width, (int)image.Size.Height, maxSize); var rect = new CGRect(0, 0, newSize.Item1, newSize.Item2); rect = rect.Integral(); UIGraphics.BeginImageContextWithOptions(new CGSize(rect.Width, rect.Height), false, image.CurrentScale); image.Draw(rect); var resizedImage = UIGraphics.GetImageFromCurrentImageContext(); UIGraphics.EndImageContext(); return(resizedImage); }