Exemple #1
0
        public async Task <WriteableBitmap> ToWriteableBitmap()
        {
            WriteableBitmap result = null;

            if (IsValid)
            {
                if (Source is WriteableBitmap)
                {
                    result = Source as WriteableBitmap;
                }
                else if (Source is BitmapSource)
                {
                    var bmp = Source as BitmapImage;
                    result = await bmp.ToWriteableBitmap();
                }
                else if (Source is SvgImageSource)
                {
                    var svg = Source as SvgImageSource;

                    if (Bytes is byte[])
                    {
                        CanvasDevice device      = CanvasDevice.GetSharedDevice();
                        var          svgDocument = new CanvasSvgDocument(device);
                        svgDocument = CanvasSvgDocument.LoadFromXml(device, Encoding.UTF8.GetString(Bytes));

                        using (var offscreen = new CanvasRenderTarget(device, (float)svg.RasterizePixelWidth, (float)svg.RasterizePixelHeight, 96))
                        {
                            var session = offscreen.CreateDrawingSession();
                            session.DrawSvg(svgDocument, new Size(Size, Size), 0, 0);
                            using (var imras = new InMemoryRandomAccessStream())
                            {
                                await offscreen.SaveAsync(imras, CanvasBitmapFileFormat.Png);

                                result = await imras.ToWriteableBitmap();
                            }
                        }
                    }
                }
            }
            return(result);
        }