public static ImageSource GetRasterizedImage(int size, string imageResourceLocation)
        {
            size *= 2;

            string key = $"{size}|{imageResourceLocation}";

            if (renders.TryGetValue(key, out ImageSource result) == false)
            {
                StreamResourceInfo streamResourceInfo;

                try
                {
                    streamResourceInfo = Application.GetResourceStream(new Uri($"pack://application:,,,/{imageResourceLocation}"));

                    if (streamResourceInfo != null)
                    {
                        var svgLoader = new Loader();
                        VectorGraphicsInfo vectorGraphicsInfo = svgLoader.LoadFromStream(streamResourceInfo.Stream);

                        result = Rasterizer.Render(size, size, vectorGraphicsInfo);
                    }

                    renders.Add(key, result);
                }
                catch
                {
                    renders.Add(key, null);
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        public static ImageSource Render(int width, int height, VectorGraphicsInfo vectorGraphicsInfo)
        {
            DpiScale    dpiInfo     = VisualTreeHelper.GetDpi(Application.Current.MainWindow);
            PixelFormat pixelFormat = PixelFormats.Pbgra32;

            var x = new RenderTargetBitmap(width, height, dpiInfo.PixelsPerInchX / dpiInfo.DpiScaleX, dpiInfo.PixelsPerInchY / dpiInfo.DpiScaleY, pixelFormat);

            var canvas = new Canvas
            {
                Width  = vectorGraphicsInfo.Viewbox.Width,
                Height = vectorGraphicsInfo.Viewbox.Height,
                SnapsToDevicePixels = false,
                UseLayoutRounding   = true
            };

            foreach (Path path in vectorGraphicsInfo.Paths)
            {
                canvas.Children.Add(path);
            }

            var viewbox = new Viewbox
            {
                Stretch             = Stretch.Fill,
                Width               = width,
                Height              = height,
                SnapsToDevicePixels = false,
                UseLayoutRounding   = true,
                Child               = canvas
            };

            var size = new Size(width, height);

            viewbox.Measure(size);
            viewbox.Arrange(new Rect(size));

            x.Render(viewbox);

            x.Freeze();

            return(x);
        }