Пример #1
0
        void TestSaveAsync(Func <CanvasDevice, ICanvasImage> createImage)
        {
            const int width  = 23;
            const int height = 42;

            var device      = new CanvasDevice();
            var sourceImage = createImage(device);
            var stream      = new MemoryStream();

            var saveTask = Task.Run(async() =>
            {
                await CanvasImage.SaveAsync(sourceImage, new Rect(0, 0, width, height), 96, device, stream.AsRandomAccessStream(), CanvasBitmapFileFormat.Png);
            });

            saveTask.Wait();

            CanvasBitmap result = null;

            var loadTask = Task.Run(async() =>
            {
                result = await CanvasBitmap.LoadAsync(device, stream.AsRandomAccessStream());
            });

            loadTask.Wait();

            Assert.AreEqual(width, result.Size.Width);
            Assert.AreEqual(height, result.Size.Height);
        }
Пример #2
0
        private async void DialController_ButtonClicked(RadialController sender, RadialControllerButtonClickedEventArgs args)
        {
            try
            {
                ShowBusyIndicator("saving file...");

                var picker = new FileSavePicker();
                picker.FileTypeChoices.Add("Jpegs", new List <string>()
                {
                    ".jpg"
                });

                var file = await picker.PickSaveFileAsync();

                if (file == null)
                {
                    return;
                }

                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    // watermark the image
                    var device = CanvasDevice.GetSharedDevice();

                    var bounds = virtualBitmap.Bounds;

                    var text = new CanvasCommandList(device);
                    using (var ds = text.CreateDrawingSession())
                    {
                        ds.DrawText("Created by Dial-In", bounds, Colors.White,
                                    new CanvasTextFormat
                        {
                            VerticalAlignment   = CanvasVerticalAlignment.Bottom,
                            HorizontalAlignment = CanvasHorizontalAlignment.Right,
                            FontFamily          = "Segoe UI",
                            FontSize            = (float)(bounds.Height / 12)
                        });
                    }

                    var effect = new BlendEffect()
                    {
                        Background = virtualBitmap,
                        Foreground = text,
                        Mode       = BlendEffectMode.Difference
                    };

                    // save the image to final file
                    await CanvasImage.SaveAsync(effect, bounds, 96, device, stream, CanvasBitmapFileFormat.Jpeg);
                }
            }
            catch (Exception ex)
            {
                await new MessageDialog($"Something went wrong saving the image: {ex}", "Exception").ShowAsync();
            }
            finally
            {
                HideBusyIndicator();
            }
        }
Пример #3
0
        public void CanvasImage_SaveAsync_HdrFormats()
        {
            var pixelFormats = new Dictionary <CanvasBufferPrecision, DirectXPixelFormat>
            {
                { CanvasBufferPrecision.Precision8UIntNormalized, DirectXPixelFormat.B8G8R8A8UIntNormalized },
                { CanvasBufferPrecision.Precision16Float, DirectXPixelFormat.R16G16B16A16Float },
                { CanvasBufferPrecision.Precision32Float, DirectXPixelFormat.R32G32B32A32Float },
                { CanvasBufferPrecision.Precision16UIntNormalized, DirectXPixelFormat.R16G16B16A16UIntNormalized },
            };

            CanvasBitmapFileFormat[] fileFormats =
            {
                CanvasBitmapFileFormat.Bmp,
                CanvasBitmapFileFormat.Gif,
                CanvasBitmapFileFormat.Jpeg,
                CanvasBitmapFileFormat.JpegXR,
                CanvasBitmapFileFormat.Png,
                CanvasBitmapFileFormat.Tiff,
            };

            var device       = new CanvasDevice();
            var sourceBitmap = new CanvasRenderTarget(device, 1, 1, 96);

            foreach (var pixelFormat in pixelFormats)
            {
                foreach (var fileFormat in fileFormats)
                {
                    bool fileFormatSupportsHdr = (fileFormat == CanvasBitmapFileFormat.JpegXR);

                    using (var stream = new MemoryStream())
                    {
                        var saveTask = CanvasImage.SaveAsync(sourceBitmap,
                                                             sourceBitmap.Bounds,
                                                             sourceBitmap.Dpi,
                                                             device,
                                                             stream.AsRandomAccessStream(),
                                                             fileFormat,
                                                             0.9f,
                                                             pixelFormat.Key).AsTask();
                        saveTask.Wait();

                        var loadTask = CanvasBitmap.LoadAsync(device, stream.AsRandomAccessStream()).AsTask();
                        loadTask.Wait();

                        var loadedBitmap = loadTask.Result;

                        var expectedFormat = pixelFormat.Value;

                        if (!fileFormatSupportsHdr || !device.IsPixelFormatSupported(expectedFormat))
                        {
                            expectedFormat = DirectXPixelFormat.B8G8R8A8UIntNormalized;
                        }

                        Assert.AreEqual(expectedFormat, loadedBitmap.Format, "File format: {0}, pixel format: {1}", fileFormat, pixelFormat);
                    }
                }
            }
        }
        public static async Task <WriteableBitmap> WriteToWriteableBitmapAsync(this ICanvasImage canvasImage,
                                                                               ICanvasResourceCreator canvasResourceCreator, Size size, float dpi)
        {
            // Initialize the in-memory stream where data will be stored.
            using (var stream = new InMemoryRandomAccessStream())
            {
                await CanvasImage.SaveAsync(canvasImage, new Rect(new Point(0, 0), size), dpi, canvasResourceCreator,
                                            stream,
                                            CanvasBitmapFileFormat.Png);

                stream.Seek(0);
                return(await BitmapFactory.FromStream(stream));
            }
        }
Пример #5
0
        private async void OnSaveAsClicked(object sender, RoutedEventArgs e)
        {
            // the button should be disabled if there's no bitmap loaded
            Debug.Assert(virtualBitmap != null);

            var picker = new FileSavePicker();

            picker.FileTypeChoices.Add("Jpegs", new List <string>()
            {
                ".jpg"
            });

            var file = await picker.PickSaveFileAsync();

            if (file == null)
            {
                return;
            }

            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                // Stamp a big "Win2D" over the image before we save it, to demonstrate
                // that the image really has been processed.
                var device = CanvasDevice.GetSharedDevice();

                var bounds = virtualBitmap.Bounds;

                var text = new CanvasCommandList(device);
                using (var ds = text.CreateDrawingSession())
                {
                    ds.DrawText("Win2D", bounds, Colors.White,
                                new CanvasTextFormat()
                    {
                        VerticalAlignment   = CanvasVerticalAlignment.Center,
                        HorizontalAlignment = CanvasHorizontalAlignment.Center,
                        FontFamily          = "Comic Sans MS",
                        FontSize            = (float)(bounds.Height / 4)
                    });
                }

                var effect = new BlendEffect()
                {
                    Background = virtualBitmap,
                    Foreground = text,
                    Mode       = BlendEffectMode.Difference
                };

                try
                {
                    await CanvasImage.SaveAsync(effect, bounds, 96, device, stream, CanvasBitmapFileFormat.Jpeg);

                    var message    = string.Format("Finished saving '{0}'", file.Name);
                    var messageBox = new MessageDialog(message, "Virtual Bitmap Example").ShowAsync();
                }
                catch
                {
                    var message    = string.Format("Error saving '{0}'", file.Name);
                    var messageBox = new MessageDialog(message, "Virtual Bitmap Example").ShowAsync();
                }
            }
        }