private async Task LoadBrushAsync()
 {
     using (IRandomAccessStream fileStream = await item.ImageFile.OpenReadAsync())
     {
         ImageEffectsBrush.LoadImageFromStream(fileStream);
     }
 }
        private async Task LoadBrushAsync()
        {
            using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
            {
                await ImageSource.ToStream(stream, BitmapEncoder.BmpEncoderId);

                stream.Seek(0);
                ImageEffectsBrush.LoadImageFromStream(stream);
            }
        }
        private async void ExportImage()
        {
            CanvasDevice device = CanvasDevice.GetSharedDevice();

            using (CanvasRenderTarget offscreen = new CanvasRenderTarget(device, item.ImageBitmap.PixelWidth, item.ImageBitmap.PixelHeight, 96))
            {
                using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
                {
                    await item.ImageBitmap.ToStream(stream, BitmapEncoder.BmpEncoderId);

                    using (CanvasBitmap image = await CanvasBitmap.LoadAsync(offscreen, stream))
                    {
                        ImageEffectsBrush.SetSource(image);

                        using (CanvasDrawingSession ds = offscreen.CreateDrawingSession())
                        {
                            ds.Clear(Windows.UI.Colors.Black);

                            var img = ImageEffectsBrush.Image;
                            ds.DrawImage(img);
                        }
                    }

                    //stream.Dispose();
                }

                using (IRandomAccessStream outstream = new InMemoryRandomAccessStream())
                {
                    WriteableBitmap writeableBitmap = new WriteableBitmap(ImageSource.PixelWidth, ImageSource.PixelHeight);

                    await offscreen.SaveAsync(outstream, CanvasBitmapFileFormat.Bmp);

                    outstream.Seek(0);

                    writeableBitmap.SetSource(outstream);

                    _LocalPersistentObject.bitmapProcessingImage = writeableBitmap;
                    ThumbnailImage.Source = _LocalPersistentObject.bitmapProcessingImage;
                }
            }
        }
        private async void ExportImage()
        {
            CanvasDevice device = CanvasDevice.GetSharedDevice();

            using (CanvasRenderTarget offscreen = new CanvasRenderTarget(
                       device, item.ImageProperties.Width, item.ImageProperties.Height, 96))
            {
                using (IRandomAccessStream stream = await item.ImageFile.OpenReadAsync())
                    using (CanvasBitmap image = await CanvasBitmap.LoadAsync(offscreen, stream, 96))
                    {
                        ImageEffectsBrush.SetSource(image);

                        using (CanvasDrawingSession ds = offscreen.CreateDrawingSession())
                        {
                            ds.Clear(Windows.UI.Colors.Black);

                            var img = ImageEffectsBrush.Image;
                            ds.DrawImage(img);
                        }

                        var fileSavePicker = new FileSavePicker()
                        {
                            SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                            SuggestedSaveFile      = item.ImageFile
                        };

                        fileSavePicker.FileTypeChoices.Add("JPEG files", new List <string>()
                        {
                            ".jpg"
                        });

                        var outputFile = await fileSavePicker.PickSaveFileAsync();

                        if (outputFile != null)
                        {
                            using (IRandomAccessStream outStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
                            {
                                await offscreen.SaveAsync(outStream, CanvasBitmapFileFormat.Jpeg);
                            }

                            // Check whether this save is overwriting the original image.
                            // If it is, replace it in the list. Otherwise, insert it as a copy.
                            bool replace = false;
                            if (outputFile.IsEqual(item.ImageFile))
                            {
                                replace = true;
                            }

                            try
                            {
                                await LoadSavedImageAsync(outputFile, replace);
                            }
                            catch (Exception ex)
                            {
                                if (ex.Message.Contains("0x80070323"))
                                {
                                    // The handle with which this oplock was associated has been closed.
                                    // The oplock is now broken. (Exception from HRESULT: 0x80070323)
                                    // This is a temporary condition, so just try again.
                                    await LoadSavedImageAsync(outputFile, replace);
                                }
                            }
                        }
                    }
            }
        }
Esempio n. 5
0
        private async void ExportImage()
        {
            // We haven't enabled image export for phone.
            var qualifiers = Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().QualifierValues;

            if (qualifiers.ContainsKey("DeviceFamily") && qualifiers["DeviceFamily"] == "Mobile")
            {
                ContentDialog notSupportedDialog = new ContentDialog
                {
                    Title           = "Export is not enabled",
                    Content         = "Image export is not enabled on phones.",
                    CloseButtonText = "OK",
                };

                ContentDialogResult result = await notSupportedDialog.ShowAsync();

                return;
            }

            // Not on phone, so export image.
            CanvasDevice device = CanvasDevice.GetSharedDevice();

            using (CanvasRenderTarget offscreen = new CanvasRenderTarget(
                       device, item.ImageProperties.Width, item.ImageProperties.Height, 96))
            {
                using (IRandomAccessStream stream = await item.ImageFile.OpenReadAsync())
                    using (CanvasBitmap image = await CanvasBitmap.LoadAsync(offscreen, stream, 96))
                    {
                        ImageEffectsBrush.SetSource(image);

                        using (CanvasDrawingSession ds = offscreen.CreateDrawingSession())
                        {
                            ds.Clear(Windows.UI.Colors.Black);

                            var img = ImageEffectsBrush.Image;
                            ds.DrawImage(img);
                        }

                        var fileSavePicker = new FileSavePicker()
                        {
                            SuggestedStartLocation = PickerLocationId.PicturesLibrary,
                            SuggestedSaveFile      = item.ImageFile
                        };

                        fileSavePicker.FileTypeChoices.Add("JPEG files", new List <string>()
                        {
                            ".jpg"
                        });

                        var outputFile = await fileSavePicker.PickSaveFileAsync();

                        if (outputFile != null)
                        {
                            using (IRandomAccessStream outStream = await outputFile.OpenAsync(FileAccessMode.ReadWrite))
                            {
                                await offscreen.SaveAsync(outStream, CanvasBitmapFileFormat.Jpeg);
                            }

                            // Check whether this save is overwriting the original image.
                            // If it is, replace it in the list. Otherwise, insert it as a copy.
                            bool replace = false;
                            if (outputFile.IsEqual(item.ImageFile))
                            {
                                replace = true;
                            }

                            try
                            {
                                await LoadSavedImageAsync(outputFile, replace);
                            }
                            catch (Exception ex)
                            {
                                if (ex.Message.Contains("0x80070323"))
                                {
                                    // The handle with which this oplock was associated has been closed.
                                    // The oplock is now broken. (Exception from HRESULT: 0x80070323)
                                    // This is a temporary condition, so just try again.
                                    await LoadSavedImageAsync(outputFile, replace);
                                }
                            }
                        }
                    }
            }
        }