Example #1
0
        public static async Task <IRandomAccessStream> ImageToJPegStreamAsync(IRandomAccessStream imageStream)
        {
            //Create a decoder for the image
            var decoder = await BitmapDecoder.CreateAsync(imageStream);

            PixelDataProvider pixelData = await decoder.GetPixelDataAsync();

            byte[] pixelBytes = pixelData.DetachPixelData();

            //
            InMemoryRandomAccessStream jpegStream = new InMemoryRandomAccessStream();

            double jpegImageQuality = 0.9;

            var propertySet  = new BitmapPropertySet();
            var qualityValue = new BitmapTypedValue(jpegImageQuality, Windows.Foundation.PropertyType.Single);

            propertySet.Add("ImageQuality", qualityValue);

            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, jpegStream, propertySet);

            //key thing here is to use decoder.OrientedPixelWidth and decoder.OrientedPixelHeight otherwise you will get garbled image on devices on some photos with orientation in metadata
            encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, decoder.OrientedPixelWidth, decoder.OrientedPixelHeight, decoder.DpiX, decoder.DpiY, pixelBytes);

            //ulong jpegImageSize = 0;
            //jpegImageSize = jpegStream.Size;

            await encoder.FlushAsync();

            await jpegStream.FlushAsync();

            return(jpegStream);
        }
Example #2
0
        public async Task <StorageFile> CropAsync()
        {
            var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("crop.jpg", CreationCollisionOption.ReplaceExisting);

            using (var fileStream = await m_imageSource.OpenAsync(FileAccessMode.Read))
                using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var decoder = await BitmapDecoder.CreateAsync(fileStream);

                    var bounds = new BitmapBounds();
                    bounds.X      = (uint)CropRectangle.X;
                    bounds.Y      = (uint)CropRectangle.Y;
                    bounds.Width  = (uint)CropRectangle.Width;
                    bounds.Height = (uint)CropRectangle.Height;

                    var transform = ComputeScalingTransformForSourceImage(decoder);
                    transform.Bounds = bounds;

                    var pixelData = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

                    var propertySet  = new BitmapPropertySet();
                    var qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);
                    propertySet.Add("ImageQuality", qualityValue);

                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);

                    encoder.SetSoftwareBitmap(pixelData);
                    await encoder.FlushAsync();
                }

            return(file);
        }
Example #3
0
        public async Task Initialize(VideoSetting videoSetting)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAndAwaitAsync(CoreDispatcherPriority.Normal, async() =>
            {
                _threadsCount   = videoSetting.UsedThreads;
                _stoppedThreads = videoSetting.UsedThreads;

                _lastFrameAdded.Start();

                _imageQuality         = new BitmapPropertySet();
                var imageQualityValue = new BitmapTypedValue(videoSetting.VideoQuality, Windows.Foundation.PropertyType.Single);
                _imageQuality.Add("ImageQuality", imageQualityValue);

                _mediaCapture = new MediaCapture();

                var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

                var settings = new MediaCaptureInitializationSettings()
                {
                    SharingMode = MediaCaptureSharingMode.ExclusiveControl,

                    //With CPU the results contain always SoftwareBitmaps, otherwise with GPU
                    //they preferring D3DSurface
                    MemoryPreference = MediaCaptureMemoryPreference.Cpu,

                    //Capture only video, no audio
                    StreamingCaptureMode = StreamingCaptureMode.Video
                };

                await _mediaCapture.InitializeAsync(settings);

                var mediaFrameSource      = _mediaCapture.FrameSources.First().Value;
                var videoDeviceController = mediaFrameSource.Controller.VideoDeviceController;

                videoDeviceController.DesiredOptimization = Windows.Media.Devices.MediaCaptureOptimization.Quality;
                videoDeviceController.PrimaryUse          = Windows.Media.Devices.CaptureUse.Video;

                //Set exposure (auto light adjustment)
                if (_mediaCapture.VideoDeviceController.Exposure.Capabilities.Supported &&
                    _mediaCapture.VideoDeviceController.Exposure.Capabilities.AutoModeSupported)
                {
                    _mediaCapture.VideoDeviceController.Exposure.TrySetAuto(true);
                }

                var videoResolutionWidthHeight = VideoResolutionWidthHeight.Get(videoSetting.VideoResolution);
                var videoSubType = VideoSubtypeHelper.Get(videoSetting.VideoSubtype);

                //Set resolution, frame rate and video subtyp
                var videoFormat = mediaFrameSource.SupportedFormats.Where(sf => sf.VideoFormat.Width == videoResolutionWidthHeight.Width &&
                                                                          sf.VideoFormat.Height == videoResolutionWidthHeight.Height &&
                                                                          sf.Subtype == videoSubType)
                                  .OrderByDescending(m => m.FrameRate.Numerator / m.FrameRate.Denominator)
                                  .First();

                await mediaFrameSource.SetFormatAsync(videoFormat);

                _mediaFrameReader = await _mediaCapture.CreateFrameReaderAsync(mediaFrameSource);
                await _mediaFrameReader.StartAsync();
            });
        }
Example #4
0
        private async Task SaveSoftwareBitmapToFile(SoftwareBitmap softwareBitmap, StorageFile outputFile)
        {
            using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                var propertySet  = new BitmapPropertySet();
                var qualityValue = new BitmapTypedValue(1.0, PropertyType.Single);
                propertySet.Add("ImageQuality", qualityValue);

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream, propertySet);

                encoder.SetSoftwareBitmap(softwareBitmap);

                try
                {
                    await encoder.FlushAsync();
                }
                catch (Exception err)
                {
                    const int WINCODEC_ERR_UNSUPPORTEDOPERATION = unchecked ((int)0x88982F81);
                    switch (err.HResult)
                    {
                    case WINCODEC_ERR_UNSUPPORTEDOPERATION:
                        // If the encoder does not support writing a thumbnail, then try again
                        // but disable thumbnail generation.
                        encoder.IsThumbnailGenerated = false;
                        break;

                    default:
                        throw;
                    }
                }
            }
        }
Example #5
0
        private async Task makeJpegAsync(BitmapDecoder decoder, byte[] pixelBytes, pdfPage page, BitmapImage bitmapImage, int x, int y)
        {
            //double jpegImageQuality = Constants.ImageAttachStartingImageQuality;
            double jpegImageQuality = 0.9;
            ulong  jpegImageSize    = 0;

            var imageWriteableStream = new InMemoryRandomAccessStream();

            //MemoryStream memoryStream = new MemoryStream();
            //var imageWriteableStream = memoryStream.AsRandomAccessStream();

            using (imageWriteableStream)
            {
                var propertySet  = new BitmapPropertySet();
                var qualityValue = new BitmapTypedValue(jpegImageQuality, Windows.Foundation.PropertyType.Single);
                propertySet.Add("ImageQuality", qualityValue);

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, imageWriteableStream, propertySet);

                //key thing here is to use decoder.OrientedPixelWidth and decoder.OrientedPixelHeight otherwise you will get garbled image on devices on some photos with orientation in metadata
                encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, decoder.OrientedPixelWidth, decoder.OrientedPixelHeight, decoder.DpiX, decoder.DpiY, pixelBytes);

                jpegImageSize = imageWriteableStream.Size;

                await encoder.FlushAsync();

                await imageWriteableStream.FlushAsync();

                var byteArray = new byte[imageWriteableStream.Size];
                await imageWriteableStream.ReadAsync(byteArray.AsBuffer(), (uint)imageWriteableStream.Size, InputStreamOptions.None);

                //page.addImage(bitmapImage, byteArray, x, y);
                page.addImage(imageWriteableStream);
            }
        }
Example #6
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);
        }
Example #7
0
        public static async Task <ImageSource> CropAndPreviewAsync(IRandomAccessStream imageStream, Rect cropRectangle)
        {
            var decoder = await BitmapDecoder.CreateAsync(imageStream);

            var bounds = new BitmapBounds();

            bounds.X      = (uint)cropRectangle.X;
            bounds.Y      = (uint)cropRectangle.Y;
            bounds.Width  = (uint)cropRectangle.Width;
            bounds.Height = (uint)cropRectangle.Height;

            var transform = ComputeScalingTransformForSourceImage(decoder);

            transform.Bounds = bounds;

            var pixelData = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

            var propertySet  = new BitmapPropertySet();
            var qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);

            propertySet.Add("ImageQuality", qualityValue);

            var bitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

            var bitmapImage = new SoftwareBitmapSource();
            await bitmapImage.SetBitmapAsync(bitmap);

            return(bitmapImage);
        }
 private BitmapPropertySet CreateBitmapPropertySet()
 {
     var propertySet = new BitmapPropertySet();
     var qualityValue = new BitmapTypedValue(configuration.VideoQuality, PropertyType.Single);
     propertySet.Add("ImageQuality", qualityValue);
     return propertySet;
 }
Example #9
0
        public async Task Save(double compression)
        {
            var picker = new FileSavePicker();

            picker.FileTypeChoices.Add("Obraz JPEG", new[] { ".jpg" });
            var file = await picker.PickSaveFileAsync();

            if (file == null || _bitmap == null)
            {
                return;
            }

            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var propertySet  = new BitmapPropertySet();
                var qualityValue = new BitmapTypedValue(compression / 10, PropertyType.Single);

                propertySet.Add("ImageQuality", qualityValue);

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream, propertySet);

                encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.NearestNeighbor;
                Stream pixelStream = _bitmap.PixelBuffer.AsStream();
                byte[] pixels      = new byte[pixelStream.Length];
                await pixelStream.ReadAsync(pixels, 0, pixels.Length);

                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
                                     (uint)_bitmap.PixelWidth, (uint)_bitmap.PixelHeight, 96, 96, pixels);

                await encoder.FlushAsync();
            }
        }
Example #10
0
        private async Task ThumbnailTranscodeAsync(UpdateFileGenerationStart update, string[] args)
        {
            try
            {
                var conversion = JsonConvert.DeserializeObject<VideoConversion>(args[2]);
                //if (conversion.Transcode)
                {
                    var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(args[0]);
                    var temp = await StorageFile.GetFileFromPathAsync(update.DestinationPath);

                    var props = await file.Properties.GetVideoPropertiesAsync();

                    double originalWidth = props.GetWidth();
                    double originalHeight = props.GetHeight();

                    //if (!conversion.CropRectangle.IsEmpty())
                    //{
                    //    file = await ImageHelper.CropAsync(file, temp, conversion.CropRectangle);
                    //    originalWidth = conversion.CropRectangle.Width;
                    //    originalHeight = conversion.CropRectangle.Height;
                    //}

                    using (var fileStream = await ImageHelper.OpenReadAsync(file))
                    using (var outputStream = await temp.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        var decoder = await BitmapDecoder.CreateAsync(fileStream);

                        double ratioX = (double)90 / originalWidth;
                        double ratioY = (double)90 / originalHeight;
                        double ratio = Math.Min(ratioX, ratioY);

                        uint width = (uint)(originalWidth * ratio);
                        uint height = (uint)(originalHeight * ratio);

                        var transform = new BitmapTransform();
                        transform.ScaledWidth = width;
                        transform.ScaledHeight = height;
                        transform.InterpolationMode = BitmapInterpolationMode.Linear;
                        transform.Flip = conversion.Mirror == MediaMirroringOptions.Horizontal ? BitmapFlip.Horizontal : BitmapFlip.None;

                        var pixelData = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

                        var propertySet = new BitmapPropertySet();
                        var qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);
                        propertySet.Add("ImageQuality", qualityValue);

                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);
                        encoder.SetSoftwareBitmap(pixelData);
                        await encoder.FlushAsync();

                        _protoService.Send(new FinishFileGeneration(update.GenerationId, null));
                    }
                }
            }
            catch (Exception ex)
            {
                _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID " + ex.ToString())));
            }
        }
Example #11
0
        /// <summary>
        /// Write the location metadata to the image file
        /// </summary>
        public async static Task WriteSurveyPhotoMetaData(StorageFile file, Geoposition pos, string surveyId = "")
        {
            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var decoder = await BitmapDecoder.CreateAsync(stream);
                var encoder = await BitmapEncoder.CreateForTranscodingAsync(stream, decoder);
                
                // Write the gps data
                var propertySet = new BitmapPropertySet();
                var latitudeNumerator = new BitmapTypedValue(CoordDoubleToIntArray(pos.Coordinate.Latitude), PropertyType.Int32Array);
                var latitudeRef = new BitmapTypedValue("N", PropertyType.String);
                var longitudeNumerator = new BitmapTypedValue(CoordDoubleToIntArray(pos.Coordinate.Longitude), PropertyType.Int32Array);
                var longitudeRef = new BitmapTypedValue("W", PropertyType.String);

                propertySet.Add("System.GPS.LatitudeNumerator", latitudeNumerator);
                propertySet.Add("System.GPS.LatitudeRef", latitudeRef);
                propertySet.Add("System.GPS.LatitudeDenominator", new BitmapTypedValue(new [] { 1, 1, 10000 }, PropertyType.Int32Array));

                propertySet.Add("System.GPS.LongitudeNumerator", longitudeNumerator);
                propertySet.Add("System.GPS.LongitudeRef", longitudeRef);
                propertySet.Add("System.GPS.LongitudeDenominator", new BitmapTypedValue(new[] { 1, 1, 10000 }, PropertyType.Int32Array));

                // Write the surveyId data
                if (!string.IsNullOrEmpty(surveyId))
                {
                    var surveyIdTyped = new BitmapTypedValue(surveyId, Windows.Foundation.PropertyType.String);
                    propertySet.Add("System.Comment", surveyIdTyped);
                }

                await encoder.BitmapProperties.SetPropertiesAsync(propertySet);
                await encoder.FlushAsync();
            }
        }
Example #12
0
        internal static async Task LoadTileImageInternalAsync(string imagePath)
        {
            string tileName = "dreaming";
            uint MaxImageWidth =360;
            uint MaxImageHeight = 600;

            StorageFile origFile = await ApplicationData.Current.LocalFolder.GetFileAsync(imagePath);

            // open file for the new tile image file
            StorageFile tileFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(tileName, CreationCollisionOption.GenerateUniqueName);
            using (IRandomAccessStream tileStream = await tileFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                // get width and height from the original image
                IRandomAccessStreamWithContentType stream = await origFile.OpenReadAsync();
                ImageProperties properties = await origFile.Properties.GetImagePropertiesAsync();
                uint width = properties.Width;
                uint height = properties.Height;

                // get proper decoder for the input file - jpg/png/gif
                BitmapDecoder decoder = await GetProperDecoder(stream, origFile );
                if (decoder == null) return; // should not happen
                // get byte array of actual decoded image
                PixelDataProvider data = await decoder.GetPixelDataAsync();
                byte[] bytes = data.DetachPixelData();

                // create encoder for saving the tile image
                BitmapPropertySet propertySet = new BitmapPropertySet();
                // create class representing target jpeg quality - a bit obscure, but it works
                BitmapTypedValue qualityValue = new BitmapTypedValue(0.5, PropertyType.Single);
                propertySet.Add("ImageQuality", qualityValue);
                // create the target jpeg decoder
                BitmapEncoder be = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, tileStream, propertySet);
                be.SetPixelData(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Straight, width, height, 96.0, 96.0, bytes);

                // crop the image, if it's too big
                if (width > MaxImageWidth || height > MaxImageHeight)
                {
                    BitmapBounds bounds = new BitmapBounds();
                    if (width > MaxImageWidth)
                    {
                        bounds.Width = MaxImageWidth;
                        bounds.X = (width - MaxImageWidth) / 2;
                    }
                    else bounds.Width = width;
                    if (height > MaxImageHeight)
                    {
                        bounds.Height = MaxImageHeight;
                        bounds.Y = (height - MaxImageHeight) / 2;
                    }
                    else bounds.Height = height;
                    be.BitmapTransform.Bounds = bounds;
                }
                // save the target jpg to the file
                await be.FlushAsync();
            }
           
        }
Example #13
0
        private async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            await Task.Delay(TimeSpan.FromSeconds(1));

            var renderTargetBitmap = new RenderTargetBitmap();
            await renderTargetBitmap.RenderAsync(Grid);

            var pngFile = await ApplicationData.Current.TemporaryFolder.CreateFileAsync(Path.GetRandomFileName() + ".jpg");

            using (var pngStream = await pngFile.OpenStreamForWriteAsync())
            {
                BitmapPropertySet propertySet  = new BitmapPropertySet();
                BitmapTypedValue  qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);
                propertySet.Add("ImageQuality", qualityValue);

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, pngStream.AsRandomAccessStream(), propertySet);

                // https://docs.microsoft.com/en-us/windows/win32/properties/windows-properties-system?WT.mc_id=WD-MVP-5003260
                propertySet = new BitmapPropertySet();
                // 作者
                propertySet.Add("System.Author", new BitmapTypedValue("lindexi", PropertyType.String));
                // 相机型号
                propertySet.Add("System.Photo.CameraModel", new BitmapTypedValue("lindexi", PropertyType.String));
                // 制造商
                propertySet.Add("System.Photo.CameraManufacturer", new BitmapTypedValue("lindexi manufacturer", PropertyType.String));
                // 光圈值 System.Photo.FNumberNumerator/System.Photo.FNumberDenominator
                propertySet.Add("System.Photo.FNumberNumerator", new BitmapTypedValue(1, PropertyType.UInt32));
                propertySet.Add("System.Photo.FNumberDenominator", new BitmapTypedValue(10, PropertyType.UInt32));

                await encoder.BitmapProperties.SetPropertiesAsync(propertySet);

                var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

                var softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(pixelBuffer, BitmapPixelFormat.Bgra8, renderTargetBitmap.PixelWidth, renderTargetBitmap.PixelHeight);
                encoder.SetSoftwareBitmap(softwareBitmap);


                await encoder.FlushAsync();

                softwareBitmap.Dispose();
            }

            await Launcher.LaunchFolderAsync(ApplicationData.Current.TemporaryFolder);
        }
        private async Task <BitmapEncoder> CaptureView(FrameworkElement view, IRandomAccessStream stream)
        {
            int w = (int)view.ActualWidth;
            int h = (int)view.ActualHeight;

            if (w <= 0 || h <= 0)
            {
                throw new InvalidOperationException("Impossible to snapshot the view: view is invalid");
            }

            RenderTargetBitmap targetBitmap = new RenderTargetBitmap();
            await targetBitmap.RenderAsync(view, w, h);

            BitmapEncoder encoder;

            if (extension != "png")
            {
                var propertySet  = new BitmapPropertySet();
                var qualityValue = new BitmapTypedValue(quality, Windows.Foundation.PropertyType.Single);
                propertySet.Add("ImageQuality", qualityValue);
                encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream, propertySet);
            }
            else
            {
                encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
            }

            var displayInformation = DisplayInformation.GetForCurrentView();
            var pixelBuffer        = await targetBitmap.GetPixelsAsync();

            encoder.SetPixelData(
                BitmapPixelFormat.Bgra8,
                BitmapAlphaMode.Ignore,
                (uint)targetBitmap.PixelWidth,
                (uint)targetBitmap.PixelHeight,
                displayInformation.LogicalDpi,
                displayInformation.LogicalDpi,
                pixelBuffer.ToArray());


            if (width != null && height != null && (width != w || height != h))
            {
                encoder.BitmapTransform.ScaledWidth = (uint)width;
                encoder.BitmapTransform.ScaledWidth = (uint)height;
            }

            if (encoder == null)
            {
                throw new InvalidOperationException("Impossible to snapshot the view");
            }

            await encoder.FlushAsync();

            return(encoder);
        }
        public async static Task <byte[]> ToByteArrayAsync(this WriteableBitmap image, Abstractions.ImageFormat format, int quality = 100)
        {
            byte[]          resultArray     = null;
            WriteableBitmap writeableBitmap = image;

            using (IRandomAccessStream ms = new InMemoryRandomAccessStream())
            {
                try
                {
                    byte[] bytes;
                    using (Stream stream = image.PixelBuffer.AsStream())
                    {
                        bytes = new byte[(uint)stream.Length];
                        await stream.ReadAsync(bytes, 0, bytes.Length);
                    }

                    BitmapPropertySet propertySet  = new BitmapPropertySet();
                    BitmapTypedValue  qualityValue = new BitmapTypedValue(
                        quality * .01,
                        Windows.Foundation.PropertyType.Single
                        );
                    propertySet.Add("ImageQuality", qualityValue);

                    BitmapEncoder encoder = null;
                    if (format == Abstractions.ImageFormat.PNG)
                    {
                        encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, ms);
                    }
                    else
                    {
                        encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ms, propertySet);
                    }

                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                         BitmapAlphaMode.Ignore,
                                         (uint)image.PixelWidth,
                                         (uint)image.PixelHeight,
                                         96,
                                         96,
                                         bytes);

                    await encoder.FlushAsync();

                    resultArray = new byte[ms.AsStream().Length];
                    await ms.AsStream().ReadAsync(resultArray, 0, resultArray.Length);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.Message);
                }
            }

            return(resultArray);
        }
Example #16
0
        /// <summary>
        /// Resizes and crops source file image so that resized image width/height are not larger than <param name="requestedMinSide"></param>
        /// </summary>
        /// <param name="sourceFile">Source StorageFile</param>
        /// <param name="resizedImageFile">Target StorageFile</param>
        /// <param name="requestedMinSide">Max width/height of the output image</param>
        /// <param name="quality">JPEG compression quality (0.77 for pictures, 0.87 for thumbnails)</param>
        /// <returns></returns>
        public static async Task <StorageFile> ScaleJpegAsync(StorageFile sourceFile, StorageFile resizedImageFile, int requestedMinSide = 1280, double quality = 0.77)
        {
            using (var imageStream = await sourceFile.OpenReadAsync())
            {
                var decoder = await BitmapDecoder.CreateAsync(imageStream);

                if (decoder.FrameCount > 1)
                {
                    throw new InvalidCastException();
                }

                var originalPixelWidth  = decoder.PixelWidth;
                var originalPixelHeight = decoder.PixelHeight;

                using (var resizedStream = await resizedImageFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    BitmapTransform transform;

                    if (decoder.PixelWidth > requestedMinSide || decoder.PixelHeight > requestedMinSide)
                    {
                        double ratioX = (double)requestedMinSide / originalPixelWidth;
                        double ratioY = (double)requestedMinSide / originalPixelHeight;
                        double ratio  = Math.Min(ratioX, ratioY);

                        uint width  = (uint)(originalPixelWidth * ratio);
                        uint height = (uint)(originalPixelHeight * ratio);

                        transform = new BitmapTransform
                        {
                            ScaledWidth       = width,
                            ScaledHeight      = height,
                            InterpolationMode = BitmapInterpolationMode.Linear
                        };
                    }
                    else
                    {
                        transform = new BitmapTransform();
                    }

                    var pixelData = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

                    var propertySet  = new BitmapPropertySet();
                    var qualityValue = new BitmapTypedValue(quality, Windows.Foundation.PropertyType.Single);
                    propertySet.Add("ImageQuality", qualityValue);

                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, resizedStream);

                    encoder.SetSoftwareBitmap(pixelData);
                    await encoder.FlushAsync();
                }
            }

            return(resizedImageFile);
        }
Example #17
0
        public static async Task <StorageFile> CropAsync(StorageFile sourceFile, StorageFile file, Rect cropRectangle, int min = 1280, int max = 0, double quality = 0.77)
        {
            if (file == null)
            {
                file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("crop.jpg", CreationCollisionOption.ReplaceExisting);
            }

            using (var fileStream = await OpenReadAsync(sourceFile))
                using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var decoder = await BitmapDecoder.CreateAsync(fileStream);

                    var cropWidth  = (double)decoder.PixelWidth;
                    var cropHeight = (double)decoder.PixelHeight;

                    if (decoder.PixelWidth > 1280 || decoder.PixelHeight > 1280)
                    {
                        double ratioX = (double)1280 / cropWidth;
                        double ratioY = (double)1280 / cropHeight;
                        double ratio  = Math.Min(ratioX, ratioY);

                        cropWidth  = cropWidth * ratio;
                        cropHeight = cropHeight * ratio;
                    }

                    var(scaledCrop, scaledSize) = Scale(cropRectangle, new Size(cropWidth, cropHeight), new Size(decoder.PixelWidth, decoder.PixelHeight), min, max);

                    var bounds = new BitmapBounds();
                    bounds.X      = (uint)scaledCrop.X;
                    bounds.Y      = (uint)scaledCrop.Y;
                    bounds.Width  = (uint)scaledCrop.Width;
                    bounds.Height = (uint)scaledCrop.Height;

                    var transform = new BitmapTransform();
                    transform.ScaledWidth       = (uint)scaledSize.Width;
                    transform.ScaledHeight      = (uint)scaledSize.Height;
                    transform.Bounds            = bounds;
                    transform.InterpolationMode = BitmapInterpolationMode.Linear;

                    var pixelData = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

                    var propertySet  = new BitmapPropertySet();
                    var qualityValue = new BitmapTypedValue(quality, PropertyType.Single);
                    propertySet.Add("ImageQuality", qualityValue);

                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);

                    encoder.SetSoftwareBitmap(pixelData);
                    await encoder.FlushAsync();
                }

            return(file);
        }
Example #18
0
        private async void SaveSoftwareBitmapToFile(SoftwareBitmap softwareBitmap, StorageFile outputFile)
        {
            using (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
            {
                // Set properties
                var propertySet  = new BitmapPropertySet();
                var qualityValue = new BitmapTypedValue(
                    1.0, // Maximum quality
                    Windows.Foundation.PropertyType.Single
                    );
                propertySet.Add("ImageQuality", qualityValue);

                // Create an encoder with the desired format
                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream, propertySet);

                // Set the software bitmap
                encoder.SetSoftwareBitmap(softwareBitmap);

                //// Set additional encoding parameters, if needed
                //encoder.BitmapTransform.ScaledWidth = 320;
                //encoder.BitmapTransform.ScaledHeight = 240;
                //encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise90Degrees;
                //encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
                encoder.IsThumbnailGenerated = true;

                try
                {
                    await encoder.FlushAsync();
                }
                catch (Exception err)
                {
                    const int WINCODEC_ERR_UNSUPPORTEDOPERATION = unchecked ((int)0x88982F81);
                    switch (err.HResult)
                    {
                    case WINCODEC_ERR_UNSUPPORTEDOPERATION:
                        // If the encoder does not support writing a thumbnail, then try again
                        // but disable thumbnail generation.
                        encoder.IsThumbnailGenerated = false;
                        break;

                    default:
                        throw;
                    }
                }

                //throwing error?
                if (encoder.IsThumbnailGenerated == false)
                {
                    await encoder.FlushAsync();
                }
            }
        }
Example #19
0
        public static async Task <EncodeResult> EncodeGif(IFrameData frameData)
        {
            var savePicker = new Windows.Storage.Pickers.FileSavePicker();

            savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
            savePicker.FileTypeChoices.Add("GIF", new List <string>()
            {
                ".gif"
            });
            savePicker.SuggestedFileName = "MyGif";

            var file = await savePicker.PickSaveFileAsync();

            if (file == null)
            {
                return new EncodeResult {
                           Success = false
                }
            }
            ;

            using (var memoryStream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.GifEncoderId, memoryStream);

                var propSet = await encoder.BitmapProperties.GetPropertiesAsync(new string[] { "/grctlext/Delay" });

                var props = new BitmapPropertySet();
                props.Add("/grctlext/Delay", new BitmapTypedValue((UInt16)10, Windows.Foundation.PropertyType.UInt16));

                var count = frameData.GetFramesCount();
                for (var frameIdx = 0; frameIdx < count; frameIdx++)
                {
                    var frame = await frameData.GetFrameToEncode(frameIdx);

                    encoder.SetSoftwareBitmap(frame.ImageSource);

                    await encoder.BitmapProperties.SetPropertiesAsync(props);

                    if (frameIdx + 1 < count)
                    {
                        await encoder.GoToNextFrameAsync();
                    }
                }

                await encoder.FlushAsync();
            }

            return(new EncodeResult {
                Success = true
            });
        }
Example #20
0
        /// <summary>
        /// Converts source image file to jpeg of defined quality (0.85)
        /// </summary>
        /// <param name="sourceFile">Source StorageFile</param>
        /// <param name="outputFile">Target StorageFile</param>
        /// <returns></returns>
        private async Task <StorageFile> ConvertImageToJpegAsync(StorageFile sourceFile, StorageFile outputFile)
        {
            //you can use WinRTXamlToolkit StorageItemExtensions.GetSizeAsync to get file size (if you already plugged this nuget in)
            var sourceFileProperties = await sourceFile.GetBasicPropertiesAsync();

            var fileSize    = sourceFileProperties.Size;
            var imageStream = await sourceFile.OpenReadAsync();

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            using (imageStream)
            {
                var decoder = await BitmapDecoder.CreateAsync(imageStream);

                var pixelData = await decoder.GetPixelDataAsync();

                var detachedPixelData = pixelData.DetachPixelData();
                pixelData = null;
                //0.85d

                //double jpegImageQuality = Constants.ImageAttachStartingImageQuality;
                double jpegImageQuality = 0.9;

                //since we're using MvvmCross, we're outputing diagnostic info to MvxTrace, you can use System.Diagnostics.Debug.WriteLine instead
                //Mvx.TaggedTrace(MvxTraceLevel.Diagnostic, "ImageService", $"Source image size: {fileSize}, trying Q={jpegImageQuality}");

                var imageWriteableStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite);

                ulong jpegImageSize = 0;
                using (imageWriteableStream)
                {
                    var propertySet  = new BitmapPropertySet();
                    var qualityValue = new BitmapTypedValue(jpegImageQuality, Windows.Foundation.PropertyType.Single);
                    propertySet.Add("ImageQuality", qualityValue);
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, imageWriteableStream, propertySet);

                    //key thing here is to use decoder.OrientedPixelWidth and decoder.OrientedPixelHeight otherwise you will get garbled image on devices on some photos with orientation in metadata
                    encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, decoder.OrientedPixelWidth, decoder.OrientedPixelHeight, decoder.DpiX, decoder.DpiY, detachedPixelData);
                    await encoder.FlushAsync();

                    await imageWriteableStream.FlushAsync();

                    jpegImageSize = imageWriteableStream.Size;
                }
                //Mvx.TaggedTrace(MvxTraceLevel.Diagnostic, "ImageService", $"Final image size now: {jpegImageSize}");
            }
            stopwatch.Stop();
            //Mvx.TaggedTrace(MvxTraceLevel.Diagnostic, "ImageService", $"Time spent optimizing image: {stopwatch.Elapsed}");
            return(outputFile);
        }
Example #21
0
        public static async Task <IBuffer> CompressImage(IRandomAccessStream stream, int reqWidth, int reqHeight)
        {
            var decoder = await BitmapDecoder.CreateAsync(stream);

            var resizedStream = new InMemoryRandomAccessStream();

            var propertySet = new BitmapPropertySet();

            propertySet.Add("ImageQuality", new BitmapTypedValue(0.9, PropertyType.Single));
            BitmapEncoder encoder =
                await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, resizedStream, propertySet);

            encoder.SetSoftwareBitmap(await decoder.GetSoftwareBitmapAsync());
            double widthRatio  = (double)reqWidth / decoder.PixelWidth;
            double heightRatio = (double)reqHeight / decoder.PixelHeight;

            double scaleRatio = Math.Min(widthRatio, heightRatio);

            if (reqWidth == 0)
            {
                scaleRatio = heightRatio;
            }

            if (reqHeight == 0)
            {
                scaleRatio = widthRatio;
            }

            uint aspectHeight = (uint)Math.Floor(decoder.PixelHeight * scaleRatio);
            uint aspectWidth  = (uint)Math.Floor(decoder.PixelWidth * scaleRatio);

            encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;

            encoder.BitmapTransform.ScaledHeight = aspectHeight;
            encoder.BitmapTransform.ScaledWidth  = aspectWidth;
            await encoder.FlushAsync();

            resizedStream.Seek(0);
            var outBuffer = new Windows.Storage.Streams.Buffer((uint)resizedStream.Size);
            await resizedStream.ReadAsync(outBuffer, (uint)resizedStream.Size, InputStreamOptions.None);

            resizedStream.Dispose();
            return(outBuffer);
        }
Example #22
0
        public async Task <BitmapEncoder> CreateOptimizedBitmapEncoderAsync(ImageScannerFormat?encoderFormat, IRandomAccessStream stream)
        {
            // get encoder ID
            Guid encoderId;

            switch (encoderFormat)
            {
            case ImageScannerFormat.Jpeg:
                encoderId = BitmapEncoder.JpegEncoderId;
                break;

            case ImageScannerFormat.Png:
                encoderId = BitmapEncoder.PngEncoderId;
                break;

            case ImageScannerFormat.Tiff:
                encoderId = BitmapEncoder.TiffEncoderId;
                break;

            case ImageScannerFormat.DeviceIndependentBitmap:
                encoderId = BitmapEncoder.BmpEncoderId;
                break;

            default:
                throw new ApplicationException($"CreateOptimizedBitmapEncoderAsync received invalid ImageScannerFormat {encoderFormat}");
            }

            // create encoder
            if (encoderFormat == ImageScannerFormat.Jpeg)
            {
                // prevent large JPG size
                var propertySet  = new BitmapPropertySet();
                var qualityValue = new BitmapTypedValue(0.85d, Windows.Foundation.PropertyType.Single);
                propertySet.Add("ImageQuality", qualityValue);

                stream.Size = 0;
                return(await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream, propertySet));
            }
            else
            {
                return(await BitmapEncoder.CreateAsync(encoderId, stream));
            }
        }
        public async Task <bool> Initialiser(uint largeur, uint hauteur, string sousTypeVideo)
        {
            bool res = false;

            _ledOccupe?.Write(GpioPinValue.High);
            _qualiteEncodageImage.Add("ImageQuality", new BitmapTypedValue(QUALITE_ENCODAGE_IMAGE, PropertyType.Single));
            _mediaCapture = new MediaCapture();
            await _mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings()
            {
                SharingMode          = MediaCaptureSharingMode.ExclusiveControl,
                MemoryPreference     = MediaCaptureMemoryPreference.Cpu,
                StreamingCaptureMode = StreamingCaptureMode.Video
            });

            res = await InitialiserSourceImages(largeur, hauteur, sousTypeVideo);

            _ledOccupe?.Write(GpioPinValue.Low);
            return(res);
        }
Example #24
0
        /// <summary>
        /// 将原来的图片转换图片质量和压缩质量
        /// </summary>
        /// <param name="sourceFile">原来的图片</param>
        /// <param name="outputFile">输出的文件</param>
        /// <param name="imageQuality">图片质量,取值范围是 0 到 1 其中 1 的质量最好,这个值设置只对 jpg 图片有效</param>
        /// <returns></returns>
        private async Task <StorageFile> ConvertImageToJpegAsync(StorageFile sourceFile, StorageFile outputFile,
                                                                 double imageQuality)
        {
            var sourceFileProperties = await sourceFile.GetBasicPropertiesAsync();

            var fileSize    = sourceFileProperties.Size;
            var imageStream = await sourceFile.OpenReadAsync();

            using (imageStream)
            {
                var decoder = await BitmapDecoder.CreateAsync(imageStream);

                var pixelData = await decoder.GetPixelDataAsync();

                var detachedPixelData    = pixelData.DetachPixelData();
                var imageWriteAbleStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite);

                using (imageWriteAbleStream)
                {
                    var propertySet = new BitmapPropertySet();
                    // 图片质量,值范围是 0到1 其中 1 的质量最好
                    var qualityValue = new BitmapTypedValue(imageQuality,
                                                            Windows.Foundation.PropertyType.Single);
                    propertySet.Add("ImageQuality", qualityValue);
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, imageWriteAbleStream,
                                                                  propertySet);

                    //key thing here is to use decoder.OrientedPixelWidth and decoder.OrientedPixelHeight otherwise you will get garbled image on devices on some photos with orientation in metadata
                    encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, decoder.OrientedPixelWidth,
                                         decoder.OrientedPixelHeight, decoder.DpiX, decoder.DpiY, detachedPixelData);
                    await encoder.FlushAsync();

                    await imageWriteAbleStream.FlushAsync();

                    var jpegImageSize = imageWriteAbleStream.Size;
                    // 欢迎访问我博客 https://blog.lindexi.com/ 里面有大量 UWP WPF 博客
                    Debug.WriteLine($"压缩之后比压缩前的文件小{fileSize - jpegImageSize}");
                }
            }

            return(outputFile);
        }
Example #25
0
        /// <summary>
        /// Saves a SoftwareBitmap to the specified StorageFile
        /// </summary>
        /// <param name="bitmap">SoftwareBitmap to save</param>
        /// <param name="file">Target StorageFile to save to</param>
        /// <returns></returns>
        private static async Task SaveSoftwareBitmapAsync(SoftwareBitmap bitmap, StorageFile file)
        {
            using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);

                var propertySet      = new BitmapPropertySet();
                var orientationValue = new BitmapTypedValue(
                    1, // Defined as EXIF orientation = "normal"
                    PropertyType.UInt16
                    );
                propertySet.Add("System.Photo.Orientation", orientationValue);

                await encoder.BitmapProperties.SetPropertiesAsync(propertySet);

                // Grab the data from the SoftwareBitmap
                encoder.SetSoftwareBitmap(bitmap);
                await encoder.FlushAsync();
            }
        }
        /// <summary>
        /// Converts SoftwareBitmap to base64 string
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static async Task <string> ConvertToBase64(SoftwareBitmap bitmap)
        {
            byte[] array  = null;
            string base64 = "";

            using (var ms = new InMemoryRandomAccessStream())
            {
                SoftwareBitmap compatibleBitmap = null;
                if (bitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                    bitmap.BitmapAlphaMode != BitmapAlphaMode.Ignore)
                {
                    compatibleBitmap = SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);
                }
                else
                {
                    compatibleBitmap = bitmap;
                }
                var encodingOptions = new BitmapPropertySet();
                encodingOptions.Add("ImageQuality", new BitmapTypedValue(
                                        1.0, // Maximum quality
                                        Windows.Foundation.PropertyType.Single));

                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ms, encodingOptions);

                encoder.SetSoftwareBitmap(compatibleBitmap);

                try
                {
                    await encoder.FlushAsync();
                }
                catch (Exception ex) { }

                array = new byte[ms.Size];
                await ms.ReadAsync(array.AsBuffer(), (uint)ms.Size, InputStreamOptions.None);

                base64 = Convert.ToBase64String(array);
                compatibleBitmap.Dispose();
            }

            return(base64);
        }
Example #27
0
    public async void SendImage(byte[] image, int width, int height, bool logResult = false)
    {
        if (connected)
        {
#if !UNITY_EDITOR
            using (var stream = new InMemoryRandomAccessStream())
            {
                var propertySet = new BitmapPropertySet();
                propertySet.Add("ImageQuality", new BitmapTypedValue(0.5, Windows.Foundation.PropertyType.Single));
                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream, propertySet);

                encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Cubic;
                encoder.BitmapTransform.ScaledHeight      = 320;
                encoder.BitmapTransform.ScaledWidth       = (uint)((float)width / height * encoder.BitmapTransform.ScaledHeight);
                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)width, (uint)height, 72, 72, image);
                try
                {
                    await encoder.FlushAsync();
                }
                catch { LoggingManager.LogError("Couldn't encode image"); }

                var data = new byte[stream.Size];
                await stream.ReadAsync(data.AsBuffer(), (uint)stream.Size, InputStreamOptions.None);

                videoOut.WriteInt32((int)stream.Size);
                await videoOut.StoreAsync();

                videoOut.WriteBytes(data);
                await videoOut.StoreAsync();

                await videoOut.FlushAsync();

                if (logResult)
                {
                    LoggingManager.Log("Sent image of size " + stream.Size + " to " + serverName + " (" + serverAddress + ")");
                }
            }
#endif
        }
    }
Example #28
0
        private async Task <byte[]> Bitmap2JPEG(SoftwareBitmap bitmap)
        {
            // JPEG quality
            var propertySet  = new BitmapPropertySet();
            var qualityValue = new BitmapTypedValue(
                0.67, // Maximum quality
                PropertyType.Single
                );

            propertySet.Add("ImageQuality", qualityValue);

            // JPEG compression into memory
            var           memStream = new InMemoryRandomAccessStream();
            BitmapEncoder encoder   = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, memStream, propertySet);

            // the SetSoftwareBitmap may cause a memory leak, but only for debugging (it's a bug in VS)
            // See http://stackoverflow.com/questions/35528030/bitmapencoder-memoryleak-with-softwarebitmap
            encoder.SetSoftwareBitmap(bitmap);
            await encoder.FlushAsync();

            // Send the data to network
            byte[] imageData;
            using (var inputStream = memStream.GetInputStreamAt(0))
            {
                using (var dataReader = new DataReader(inputStream))
                {
                    // Once we have written the contents successfully we load the stream.
                    await dataReader.LoadAsync((uint)memStream.Size);

                    imageData = new byte[(uint)memStream.Size];
                    dataReader.ReadBytes(imageData);
                }
            }

            // Clean...
            memStream.Dispose();

            return(imageData);
        }
        public static async Task <Stream> AsJpegStreamAsync(this WriteableBitmap bitmap, int quality = 90)
        {
            byte[] pixels;
            using (var stream = bitmap.PixelBuffer.AsStream())
            {
                pixels = new byte[(uint)stream.Length];
                await stream.ReadAsync(pixels, 0, pixels.Length);
            }

            var propertySet  = new BitmapPropertySet();
            var qualityValue = new BitmapTypedValue((double)quality / 100d, Windows.Foundation.PropertyType.Single);

            propertySet.Add("ImageQuality", qualityValue);

            var raStream = new InMemoryRandomAccessStream();
            // Encode pixels into stream
            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, raStream, propertySet);

            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96, 96, pixels);
            await encoder.FlushAsync();

            return(raStream.AsStreamForRead());
        }
Example #30
0
        /// <summary>
        /// Method that will generate an ObjectState for the provided deck information.
        /// </summary>
        /// <param name="deckCardIds">List of strings representing card Ids.</param>
        /// <param name="strBackCard">Direct image url representing the back of the card.</param>
        /// <returns>A ObjectState representing the deck.</returns>
        private async Task <ObjectState> generateDeck(List <string> deckCardIds, string strBackCard)
        {
            // Generate WriteableBitmaps for deck.
            Guid            BitmapEncoderGuid = BitmapEncoder.JpegEncoderId;
            WriteableBitmap deckBitmap        = await generateWriteableBitmapForDeck(deckCardIds);

            //var file = await Windows.Storage.ApplicationData.Current.TemporaryFolder.CreateFileAsync("combine.jpg", CreationCollisionOption.ReplaceExisting);
            using (IRandomAccessStream stream = new InMemoryRandomAccessStream())//await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                BitmapPropertySet propertySet = new BitmapPropertySet();
                BitmapTypedValue  quality     = new BitmapTypedValue(1.0, PropertyType.Single);
                propertySet.Add("ImageQuality", quality);
                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream, propertySet);

                Stream pixelStream = deckBitmap.PixelBuffer.AsStream();
                byte[] pixels      = new byte[pixelStream.Length];
                await pixelStream.ReadAsync(pixels, 0, pixels.Length);

                encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)deckBitmap.PixelWidth, (uint)deckBitmap.PixelHeight, 96, 96, pixels);
                await encoder.FlushAsync();

                // Upload WriteableBitmaps to Imgur and save their direct urls (requires Imgur API).
                string deckUrl = await uploadPhotoToImgur(stream);

                // Get list of card names for deck.
                List <string> deckCardNames = await generateCardNamesForDeck(deckCardIds);

                // Get the number of rows and columns that are in the WriteableBitmap.
                Tuple <int, int> deckSize = getDeckSize(deckCardIds);

                // Build out ObjectState objects for deck.
                ObjectState deckObjectState = buildObjectState(deckCardIds, deckSize, deckUrl, strBackCard, deckCardNames, 1);

                return(deckObjectState);
            }
        }
        private async Task <Stream> ConvertToJpeg(IRandomAccessStream stream)
        {
            var decoder = await BitmapDecoder.CreateAsync(stream);

            var pixels = await decoder.GetPixelDataAsync();

            var outStream = new InMemoryRandomAccessStream();
            // create encoder for saving the tile image
            var propertySet = new BitmapPropertySet();
            // create class representing target jpeg quality - a bit obscure, but it works
            var qualityValue = new BitmapTypedValue(.7, PropertyType.Single);

            propertySet.Add("ImageQuality", qualityValue);

            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outStream);

            encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore,
                                 decoder.OrientedPixelWidth, decoder.OrientedPixelHeight,
                                 decoder.DpiX, decoder.DpiY,
                                 pixels.DetachPixelData());
            await encoder.FlushAsync();

            return(outStream.AsStream());
        }
        public async Task<string> GetImageAsBase64Encoded()
        {
            // Adapted from
            // http social.msdn.microsoft.com/Forums/wpapps/en-US/.../image-to-base64-encoding [Win Phone 7, 2010] and
            // <same site> /sharing-a-writeablebitmap and
            // www.charlespetzold.com/blog/2012/08/WritableBitmap-Pixel-Arrays-in-CSharp-and-CPlusPlus.html
            // example in WebcamPage.xaml.cs
            string results = "";
            try
            {
                //using (Stream stream = App.CurrentPatient.ImageWriteableBitmap.PixelBuffer.AsStream())
                using (Stream stream = ImageWriteableBitmap.PixelBuffer.AsStream()) //There are 4 bytes per pixel. 
                {
                    byte[] pixels = await ConvertStreamToByteArray(stream);

                    // Now render the object in a known format, e.g., jpeg:
                    //InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
                    IRandomAccessStream ms = new InMemoryRandomAccessStream();
                    // If calling from a synchronous function, add AsTask().ConfigureAwait(false) to avoid hanging UI thread

                    // If image as optionally cropped is over 1.5Mp, then apply aggressive compression to get it to that size (approximately)
                    double quality = 1.0;  //Max quality, the default.  For jpeg, 1.0 - quality = compression ratio
                    UInt64 totalPixels = (ulong)ImageWriteableBitmap.PixelWidth * (ulong)ImageWriteableBitmap.PixelHeight;
                    if (totalPixels > 1500000L)
                        quality = Math.Round(((double)1500000.0 / (double)totalPixels), 2, MidpointRounding.AwayFromZero); // for debug purposes, round quality to 2 fractional digits
                    // For encoding options, wee msdn.microsoft.com/en-us/library/windows/apps/jj218354.aspx "How to use encoding options".
                    var encodingOptions = new BitmapPropertySet();
                    var qualityValue = new BitmapTypedValue(quality, Windows.Foundation.PropertyType.Single);
                    encodingOptions.Add("ImageQuality", qualityValue);

                    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, ms, encodingOptions); //.AsTask().ConfigureAwait(false);
                    /*
                                    BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, ms); */
                    encoder.SetPixelData(
                        BitmapPixelFormat.Bgra8, //Rgba8, //Bgra8,
                        BitmapAlphaMode.Straight, // .Ignore
                        (uint)ImageWriteableBitmap.PixelWidth,//(uint)App.CurrentPatient.ImageWriteableBitmap.PixelWidth,
                        (uint)ImageWriteableBitmap.PixelHeight,//(uint)App.CurrentPatient.ImageWriteableBitmap.PixelHeight,
                        96.0, 96.0, pixels);

                    await encoder.FlushAsync(); //.AsTask().ConfigureAwait(false);
                    byte[] jpgEncoded = await ConvertMemoryStreamToByteArray(ms);
                    results = Convert.ToBase64String(jpgEncoded);
                    await ms.FlushAsync(); // cleanup
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error when encoding image: " + ex.Message);
            }
            return results;
        }
Example #33
0
       public static async Task<string> HttpPost(StorageFile inFile)
       {
           HttpClient httpClient = new HttpClient();
           uint MaxImageWidth = 480;
           uint MaxImageHeight = 800;
           uint width;
           uint height;
           string posturi = Config.apiChatImage;
           HttpMultipartFormDataContent fileContent = new HttpMultipartFormDataContent();

                        var inStream = await inFile.OpenReadAsync();
                        InMemoryRandomAccessStream outStream = new InMemoryRandomAccessStream();
                        BitmapDecoder decoder = await ImageHelp.GetProperDecoder(inStream,inFile);
                       


                        if (decoder.OrientedPixelWidth > MaxImageWidth && decoder.OrientedPixelHeight > MaxImageHeight)
                        {
                            width = MaxImageWidth;
                            height = MaxImageHeight;
                        }
                        else
                        {
                            width = decoder.OrientedPixelWidth;
                            height = decoder.OrientedPixelHeight;
                        }

                        


                        PixelDataProvider provider = await decoder.GetPixelDataAsync();
                        byte[] data = provider.DetachPixelData(); //获取像素数据的字节数组
                               

                        BitmapPropertySet propertySet = new BitmapPropertySet();
                        BitmapTypedValue qualityValue = new BitmapTypedValue(0.5, PropertyType.Single);
                        propertySet.Add("ImageQuality", qualityValue);


                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outStream, propertySet);//建立编码器
                                encoder.SetPixelData(decoder.BitmapPixelFormat,
                                                     decoder.BitmapAlphaMode,
                                                     decoder.OrientedPixelWidth,
                                                     decoder.OrientedPixelHeight,
                                                      decoder.DpiX,
                                                      decoder.DpiY,
                                                      data
                                    );

                                encoder.BitmapTransform.ScaledWidth = width;
                                encoder.BitmapTransform.ScaledHeight = height;
                                await encoder.FlushAsync(); 
                                HttpStreamContent streamContent1 = new HttpStreamContent(outStream);
                                fileContent.Add(streamContent1, "pic", inFile.Name);
                                HttpResponseMessage response = await httpClient.PostAsync(new Uri(posturi), fileContent);
                             
                             return await response.Content.ReadAsStringAsync();
       
       
       
       
       }
Example #34
0
        private async Task WriteToDiskAsync()
        {
            var diskWriteInputPackages = CollectedColorFrames.Concat(CollectedIlluminatedInfraredFrames).Concat(CollectedNonIlluminatedInfraredFrames)
                                         .AsParallel().Select(async frame =>
            {
                SoftwareBitmap compatibleBitmap = null;
                Action cleanupAction            = () => { };
                if (frame.SoftwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                    frame.SoftwareBitmap.BitmapAlphaMode != BitmapAlphaMode.Ignore)
                {
                    compatibleBitmap = SoftwareBitmap.Convert(frame.SoftwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);
                    cleanupAction    = () => compatibleBitmap.Dispose();
                }
                else
                {
                    compatibleBitmap = frame.SoftwareBitmap;
                }

                var fileName        = ((ulong)frame.SystemRelativeTime?.Ticks).ToString("D10");
                var encodingOptions = new BitmapPropertySet();
                var encoderId       = Guid.Empty;

                if (frame.SourceKind == MediaFrameSourceKind.Color)
                {
                    fileName  = "RGB-" + fileName + ".jpg";
                    encoderId = BitmapEncoder.JpegEncoderId;
                    encodingOptions.Add("ImageQuality", new BitmapTypedValue(
                                            1.0, // Maximum quality
                                            Windows.Foundation.PropertyType.Single));
                }
                else if (frame.SourceKind == MediaFrameSourceKind.Infrared)
                {
                    if (frame.IsIlluminated == true)
                    {
                        fileName = "IIR-" + fileName;  // Illuminated IR
                    }
                    else
                    {
                        fileName = "AIR-" + fileName;  // Ambient IR
                    }

                    fileName += ".png";
                    encoderId = BitmapEncoder.PngEncoderId;
                }

                var memoryStream      = new InMemoryRandomAccessStream();
                BitmapEncoder encoder = await BitmapEncoder.CreateAsync(encoderId, memoryStream, encodingOptions);
                encoder.SetSoftwareBitmap(compatibleBitmap);
                await encoder.FlushAsync();
                return(new { Stream = memoryStream.AsStream(), CleanupAction = cleanupAction, FileName = fileName });
            })
                                         .Select(task => task.ToObservable()).Merge().ObserveOn(NewThreadScheduler.Default).ToEnumerable(); // sequentialize awaitables

            var zipPath = Path.Combine(WorkingFolder.Path, "CollectedData.zip");

            using (FileStream zipFileStream = new FileStream(zipPath, FileMode.Create))
            {
                using (ZipArchive archive = new ZipArchive(zipFileStream, ZipArchiveMode.Create))
                {
                    foreach (var input in diskWriteInputPackages)
                    {
                        ZipArchiveEntry zipImgFileEntry = archive.CreateEntry(input.FileName, CompressionLevel.NoCompression);
                        using (Stream zipEntryStream = zipImgFileEntry.Open())
                        {
                            await input.Stream.CopyToAsync(zipEntryStream);
                        }

                        ++WrittenToDiskFramesCount;
                    }

                    ZipArchiveEntry zipMetadataFileEntry = archive.CreateEntry("info.json", CompressionLevel.NoCompression);
                    using (Stream zipEntryStream = zipMetadataFileEntry.Open())
                    {
                        MetadataWriter.WriteSerialization(zipEntryStream.AsOutputStream(), MediaCapture.Properties);
                    }
                }
            }

            foreach (var input in diskWriteInputPackages)
            {
                input.CleanupAction();
                input.Stream.Dispose();
            }
        }
Example #35
0
        /// <summary>
        /// Encodes the specified bitmap data and outputs it to the specified
        /// <c>BinaryWriter</c>. Bitmap data should be in BGRA format.
        /// For internal use only.
        /// </summary>
        public async Task EncodeAsync(byte[] bytes, BinaryWriter writer)
        {
#if NETFX_CORE
            using (var jpegStream = new InMemoryRandomAccessStream())
            {
                var propertySet = new BitmapPropertySet();
                var qualityValue = new BitmapTypedValue(this.JpegQuality / 100.0, PropertyType.Single);
                propertySet.Add("ImageQuality", qualityValue);

                var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, jpegStream, propertySet);
                if (this.Width != this.OutputWidth)
                {
                    encoder.BitmapTransform.ScaledWidth = (uint)this.OutputWidth;
                    encoder.BitmapTransform.ScaledHeight = (uint)this.OutputHeight;
                }

                encoder.SetPixelData(
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Straight,
                    (uint)this.Width,
                    (uint)this.Height,
                    96,
                    96,
                    bytes);
                await encoder.FlushAsync();

                if (writer.BaseStream == null || writer.BaseStream.CanWrite == false)
                    return;

                // Header
                writer.Write(this.OutputWidth);
                writer.Write(this.OutputHeight);
                writer.Write((int)jpegStream.Size);

                // Data
                jpegStream.AsStreamForRead().CopyTo(writer.BaseStream);
            }
#else
            await Task.Run(() =>
            {
                var format = PixelFormats.Bgra32;
                int stride = (int)this.Width * format.BitsPerPixel / 8;
                var bmp = BitmapSource.Create(
                    this.Width,
                    this.Height,
                    96.0,
                    96.0,
                    format,
                    null,
                    bytes,
                    stride);
                BitmapFrame frame;
                if (this.Width != this.OutputWidth || this.Height != this.OutputHeight)
                {
                    var transform = new ScaleTransform((double)this.OutputHeight / this.Height, (double)this.OutputHeight / this.Height);
                    var scaledbmp = new TransformedBitmap(bmp, transform);
                    frame = BitmapFrame.Create(scaledbmp);
                }
                else
                {
                    frame = BitmapFrame.Create(bmp);
                }

                var encoder = new JpegBitmapEncoder()
                {
                    QualityLevel = this.JpegQuality
                };
                encoder.Frames.Add(frame);
                using (var jpegStream = new MemoryStream())
                {
                    encoder.Save(jpegStream);

                    if (writer.BaseStream == null || writer.BaseStream.CanWrite == false)
                        return;

                    // Header
                    writer.Write(this.OutputWidth);
                    writer.Write(this.OutputHeight);
                    writer.Write((int)jpegStream.Length);

                    // Data
                    jpegStream.Position = 0;
                    jpegStream.CopyTo(writer.BaseStream);
                }
            });
#endif
        }
        private async Task<Stream> ConvertToJpeg(IRandomAccessStream stream)
        {
            var decoder = await BitmapDecoder.CreateAsync(stream);
            var pixels = await decoder.GetPixelDataAsync();

            var outStream = new InMemoryRandomAccessStream();
            // create encoder for saving the tile image
            var propertySet = new BitmapPropertySet();
            // create class representing target jpeg quality - a bit obscure, but it works
            var qualityValue = new BitmapTypedValue(.7, PropertyType.Single);
            propertySet.Add("ImageQuality", qualityValue);

            var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outStream);
            encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore,
                decoder.OrientedPixelWidth, decoder.OrientedPixelHeight,
                decoder.DpiX, decoder.DpiY,
                pixels.DetachPixelData());
            await encoder.FlushAsync();
            return outStream.AsStream();
        }
        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;
        }
Example #38
0
        public static async Task <TLPhotoSizeBase> GetVideoThumbnailAsync(StorageFile file, VideoProperties props, VideoTransformEffectDefinition effect)
        {
            double originalWidth  = props.GetWidth();
            double originalHeight = props.GetHeight();

            if (effect != null && !effect.CropRectangle.IsEmpty)
            {
                file = await CropAsync(file, effect.CropRectangle);

                originalWidth  = effect.CropRectangle.Width;
                originalHeight = effect.CropRectangle.Height;
            }

            TLPhotoSizeBase result;
            var             fileLocation = new TLFileLocation
            {
                VolumeId = TLLong.Random(),
                LocalId  = TLInt.Random(),
                Secret   = TLLong.Random(),
                DCId     = 0
            };

            var desiredName = string.Format("{0}_{1}_{2}.jpg", fileLocation.VolumeId, fileLocation.LocalId, fileLocation.Secret);
            var desiredFile = await FileUtils.CreateTempFileAsync(desiredName);

            using (var fileStream = await OpenReadAsync(file))
                using (var outputStream = await desiredFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var decoder = await BitmapDecoder.CreateAsync(fileStream);

                    double ratioX = (double)90 / originalWidth;
                    double ratioY = (double)90 / originalHeight;
                    double ratio  = Math.Min(ratioX, ratioY);

                    uint width  = (uint)(originalWidth * ratio);
                    uint height = (uint)(originalHeight * ratio);

                    var transform = new BitmapTransform();
                    transform.ScaledWidth       = width;
                    transform.ScaledHeight      = height;
                    transform.InterpolationMode = BitmapInterpolationMode.Linear;

                    if (effect != null)
                    {
                        transform.Flip = effect.Mirror == MediaMirroringOptions.Horizontal ? BitmapFlip.Horizontal : BitmapFlip.None;
                    }

                    var pixelData = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

                    var propertySet  = new BitmapPropertySet();
                    var qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);
                    propertySet.Add("ImageQuality", qualityValue);

                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);

                    encoder.SetSoftwareBitmap(pixelData);
                    await encoder.FlushAsync();

                    result = new TLPhotoSize
                    {
                        W        = (int)width,
                        H        = (int)height,
                        Size     = (int)outputStream.Size,
                        Type     = string.Empty,
                        Location = fileLocation
                    };
                }

            return(result);
        }
Example #39
0
        /// <summary>
        /// Applies the user-provided scale and rotation operation to the original image file.
        /// The "Transcoding" mode is used which preserves image metadata and performs other
        /// optimizations when possible.
        /// 
        /// This method attempts to perform "soft" rotation using the EXIF orientation flag when possible,
        /// but falls back to a hard rotation of the image pixels.
        /// </summary>
        private async void Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                rootPage.NotifyUser("Saving file...", NotifyType.StatusMessage);

                // Create a new encoder and initialize it with data from the original file.
                // The encoder writes to an in-memory stream, we then copy the contents to the file.
                // This allows the application to perform in-place editing of the file: any unedited data
                // is copied to the destination, and the original file is overwritten
                // with updated data.
                StorageFile file = await m_futureAccess.GetFileAsync(m_fileToken);
                using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite),
                                           memStream = new InMemoryRandomAccessStream())
                {
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

                    // Use the native (no orientation applied) image dimensions because we want to handle
                    // orientation ourselves.
                    uint originalWidth = decoder.PixelWidth;
                    uint originalHeight = decoder.PixelHeight;

                    // Set the encoder's destination to the temporary, in-memory stream.
                    BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder);

                    // Scaling occurs before flip/rotation, therefore use the original dimensions
                    // (no orientation applied) as parameters for scaling.
                    // Dimensions are rounded down by BitmapEncoder to the nearest integer.
                    if (m_scaleFactor != 1.0)
                    {
                        encoder.BitmapTransform.ScaledWidth = (uint)(originalWidth * m_scaleFactor);
                        encoder.BitmapTransform.ScaledHeight = (uint)(originalHeight * m_scaleFactor);

                        // Fant is a relatively high quality interpolation mode.
                        encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
                    }

                    // If the file format supports EXIF orientation ("System.Photo.Orientation") then
                    // update the orientation flag to reflect any user-specified rotation.
                    // Otherwise, perform a hard rotate using BitmapTransform.
                    if (m_disableExifOrientation == false)
                    {
                        BitmapPropertySet properties = new BitmapPropertySet();
                        ushort netExifOrientation = Helpers.ConvertToExifOrientationFlag(
                            Helpers.AddPhotoOrientation(m_userRotation, m_exifOrientation)
                            );

                        // BitmapProperties requires the application to explicitly declare the type
                        // of the property to be written - this is different from FileProperties which
                        // automatically coerces the value to the correct type. System.Photo.Orientation
                        // is defined as an unsigned 16 bit integer.
                        BitmapTypedValue orientationTypedValue = new BitmapTypedValue(
                            netExifOrientation,
                            Windows.Foundation.PropertyType.UInt16
                            );

                        properties.Add("System.Photo.Orientation", orientationTypedValue);
                        await encoder.BitmapProperties.SetPropertiesAsync(properties);
                    }
                    else
                    {
                        encoder.BitmapTransform.Rotation = Helpers.ConvertToBitmapRotation(m_userRotation);
                    }

                    // Attempt to generate a new thumbnail to reflect any rotation operation.
                    encoder.IsThumbnailGenerated = true;

                    try
                    {
                        await encoder.FlushAsync();
                    }
                    catch (Exception err)
                    {
                        switch (err.HResult)
                        {
                            case WINCODEC_ERR_UNSUPPORTEDOPERATION:
                                // If the encoder does not support writing a thumbnail, then try again
                                // but disable thumbnail generation.
                                encoder.IsThumbnailGenerated = false;
                                break;
                            default:
                                throw;
                        }
                    }

                    if (encoder.IsThumbnailGenerated == false)
                    {
                        await encoder.FlushAsync();
                    }

                    // Now that the file has been written to the temporary stream, copy the data to the file.
                    memStream.Seek(0);
                    fileStream.Seek(0);
                    fileStream.Size = 0;
                    await RandomAccessStream.CopyAsync(memStream, fileStream);
                }

                // Because the original file has been overwritten, reload it in the UI.
                ResetSessionState();
                await DisplayImageFileAsync(file);

                rootPage.NotifyUser("Successfully saved file: " + file.Name, NotifyType.StatusMessage);
            }
            catch (Exception err)
            {
                if (err.HResult == WINCODEC_ERR_COMPONENTNOTFOUND)
                {
                    // Some image formats (e.g. ICO) do not have encoders.
                    rootPage.NotifyUser("Error: this file format may not support editing.", NotifyType.ErrorMessage);
                }
                else
                {
                    rootPage.NotifyUser("Error: " + err.Message, NotifyType.ErrorMessage);
                }

                ResetPersistedState();
                ResetSessionState();
            }
        }
Example #40
0
        public  async void HttpPost(string uid, string uimage, string name, string content, List<string> imagePsthList, string songPath,string date,int type)
        {
            NotifyControl notify = new NotifyControl();
            notify.Text = "亲,努力发送中...";
            notify.Show();
            HttpClient httpClient = new HttpClient();
            uint MaxImageWidth = 480;
            uint MaxImageHeight = 800;
            uint width;
            uint height;
            try
            {
                string posturi = Config.apiDreamingPublish;
                HttpMultipartFormDataContent fileContent = new HttpMultipartFormDataContent();
                if (imagePsthList.Count > 0)
                {
                    for (int i = 0; i < imagePsthList.Count; i++)
                    {
                        StorageFile inFile = await StorageFile.GetFileFromPathAsync(imagePsthList[i]);
                        var inStream = await inFile.OpenReadAsync();
                        InMemoryRandomAccessStream outStream = new InMemoryRandomAccessStream();
                        BitmapDecoder decoder = await ImageHelp.GetProperDecoder(inStream,inFile);
                        if (decoder == null) return;


                        if (decoder.OrientedPixelWidth > MaxImageWidth && decoder.OrientedPixelHeight > MaxImageHeight)
                        {
                            width = MaxImageWidth;
                            height = MaxImageHeight;
                        }
                        else
                        {
                            width = decoder.OrientedPixelWidth;
                            height = decoder.OrientedPixelHeight;
                        }

                        


                        PixelDataProvider provider = await decoder.GetPixelDataAsync();
                        byte[] data = provider.DetachPixelData(); //获取像素数据的字节数组
                               

                        BitmapPropertySet propertySet = new BitmapPropertySet();
                        BitmapTypedValue qualityValue = new BitmapTypedValue(0.5, PropertyType.Single);
                        propertySet.Add("ImageQuality", qualityValue);


                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outStream, propertySet);//建立编码器
                                encoder.SetPixelData(decoder.BitmapPixelFormat,
                                                     decoder.BitmapAlphaMode,
                                                     decoder.OrientedPixelWidth,
                                                     decoder.OrientedPixelHeight,
                                                      decoder.DpiX,
                                                      decoder.DpiY,
                                                      data
                                    );
                         
                                encoder.BitmapTransform.ScaledWidth = width;
                                encoder.BitmapTransform.ScaledHeight = height;
                                await encoder.FlushAsync(); 
                                HttpStreamContent streamContent1 = new HttpStreamContent(outStream);
                                fileContent.Add(streamContent1, "pic", inFile.Name);
                              }
                          }

              
                if (songPath != null)
                {
                    StorageFile file1 = await StorageFile.GetFileFromPathAsync(songPath);
                    IRandomAccessStreamWithContentType stream1 = await file1.OpenReadAsync();
                    HttpStreamContent streamContent1 = new HttpStreamContent(stream1);
                    fileContent.Add(streamContent1, "song", file1.Name);
                }

                HttpStringContent stringContent = new HttpStringContent(content);
                HttpStringContent stringName = new HttpStringContent(name);
                HttpStringContent stringUid = new HttpStringContent(uid);
                HttpStringContent stringUimage = new HttpStringContent(uimage);
                HttpStringContent stringTime = new HttpStringContent(date);
                HttpStringContent stringType = new HttpStringContent(type.ToString());
                

                fileContent.Add(stringContent, "content");
                fileContent.Add(stringUid, "phone");
                fileContent.Add(stringUimage, "uimage");

                fileContent.Add(stringName, "name");
                fileContent.Add(stringTime, "time");
                fileContent.Add(stringType, "type");

                HttpResponseMessage response = await httpClient.PostAsync(new Uri(posturi), fileContent);
              
                Init();
                notify.Hide();
                PostCommand.CanExecutes = true;
                NavigationHelp.NavigateTo(typeof(AllDreaming));
                



            }
            catch (Exception ex)
            {
                HelpMethods.Msg(ex.Message.ToString());
              
            }

        }