Exemple #1
0
        private async Task <IRandomAccessStream> ResizeJpegStreamAsync(int maxPixelDimension, int percentQuality, IRandomAccessStream input)
        {
            var decoder = await BitmapDecoder.CreateAsync(input);

            int targetHeight;
            int targetWidth;

            MvxPictureDimensionHelper.TargetWidthAndHeight(maxPixelDimension, (int)decoder.PixelWidth, (int)decoder.PixelHeight, out targetWidth, out targetHeight);

            var transform = new BitmapTransform()
            {
                ScaledHeight = (uint)targetHeight, ScaledWidth = (uint)targetWidth
            };
            var pixelData = await decoder.GetPixelDataAsync(
                BitmapPixelFormat.Rgba8,
                BitmapAlphaMode.Straight,
                transform,
                ExifOrientationMode.RespectExifOrientation,
                ColorManagementMode.DoNotColorManage);

            var destinationStream   = new InMemoryRandomAccessStream();
            var bitmapPropertiesSet = new BitmapPropertySet();

            bitmapPropertiesSet.Add("ImageQuality", new BitmapTypedValue(((double)percentQuality) / 100.0, PropertyType.Single));
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, destinationStream, bitmapPropertiesSet);

            encoder.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied, (uint)targetWidth, (uint)targetHeight, decoder.DpiX, decoder.DpiY, pixelData.DetachPixelData());
            await encoder.FlushAsync();

            destinationStream.Seek(0L);
            return(destinationStream);
        }
Exemple #2
0
        public void ChoosePictureFromLibrary(int maxPixelDimension, int percentQuality, Action <Stream, string> pictureAvailable, Action assumeCancelled)
        {
            var filePicker = new OpenFileDialog();

            filePicker.Filter      = "Image Files (*.jpg,*.jpeg,*.gif,*.png)|*.jpg;*.jpeg;*.gif;*.png";
            filePicker.Multiselect = false;

            if (filePicker.ShowDialog() == true)
            {
                try
                {
                    var bm = new Bitmap(filePicker.FileName);
                    if (bm != null)
                    {
                        int targetWidth;
                        int targetHeight;

                        MvxPictureDimensionHelper.TargetWidthAndHeight(maxPixelDimension, bm.Width, bm.Height, out targetWidth, out targetHeight);
                        var transformBM = new TransformedBitmap(ConvertBitmapInBitmapSource(bm), new ScaleTransform(targetWidth / (double)bm.Width, targetHeight / (double)bm.Height));

                        var encoder = GetBitmapEncoder(filePicker.FileName, percentQuality);
                        if (encoder == null)
                        {
                            throw new NotSupportedException("The image file is invalid. Please, select a jpg, jpeg, gif, png or tiff file.");
                        }

                        MemoryStream stream = new MemoryStream();
                        encoder.Frames.Add(BitmapFrame.Create(transformBM));
                        encoder.Save(stream);

                        stream.Position = 0;

                        pictureAvailable(stream, filePicker.FileName);
                    }
                }
                catch (ArgumentException)
                {
                    assumeCancelled();
                }
            }
            else
            {
                assumeCancelled();
            }
        }
        private void ResizeJpegStream(int maxPixelDimension, int percentQuality, Stream input, Action <Stream> success)
        {
            var bitmap = new BitmapImage();

            bitmap.SetSource(input);
            var writeable = new WriteableBitmap(bitmap);
            int targetHeight;
            int targetWidth;

            MvxPictureDimensionHelper.TargetWidthAndHeight(maxPixelDimension, writeable.PixelWidth, writeable.PixelHeight, out targetWidth, out targetHeight);

            // not - important - we do *not* use using here - disposing of memoryStream is someone else's problem
            var memoryStream = new MemoryStream();

            writeable.SaveJpeg(memoryStream, targetWidth, targetHeight, 0, percentQuality);
            memoryStream.Seek(0L, SeekOrigin.Begin);
            success(memoryStream);
        }
Exemple #4
0
        private async Task<IRandomAccessStream> ResizeJpegStreamAsyncRubbish(int maxPixelDimension, int percentQuality, IRandomAccessStream input)
        {
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(input);

            // create a new stream and encoder for the new image
            var ras = new InMemoryRandomAccessStream();
            var enc = await BitmapEncoder.CreateForTranscodingAsync(ras, decoder);

            int targetHeight;
            int targetWidth;
            MvxPictureDimensionHelper.TargetWidthAndHeight(maxPixelDimension, (int)decoder.PixelWidth, (int)decoder.PixelHeight, out targetWidth, out targetHeight);

            enc.BitmapTransform.ScaledHeight = (uint)targetHeight;
            enc.BitmapTransform.ScaledWidth = (uint)targetWidth;

            // write out to the stream
            await enc.FlushAsync();

            return ras;
        }