Shradha
Shradha

Reputation: 90

Drawing an image from svg url in wpf C#

This is the URL with image-https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/acid.svg

What I have tried Sample 1- -I am using SharpVectors package

public void DrawImage()
{
            Image svgImage = new Image();
            WpfDrawingSettings settings = new WpfDrawingSettings();
            settings.IncludeRuntime = false;
            settings.TextAsGeometry = true;

            FileSvgReader converter = new FileSvgReader(settings);
            DrawingGroup drawing = converter.Read(new Uri("https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/acid.svg"));

            if (drawing != null)
            {
                svgImage.Source = new DrawingImage(drawing);
            }
}

sample 1 works perfectly but it's latency is very high, So I wanted to improve the performance.

Sample 2 - I tried using bitmap to improve performance below is code, but the issue is I need to download the image or store in folder and use as path as string which works fine but my condition is my URLs are coming form JSON file in .SVG format, they are dynamic and I am not using any .XAMl files for UI. UI built in code behind programmatically. Is there any other way I can display SVG image in WPF?

public void DrawImage()
{
            Image dynamicImage = new Image();
            dynamicImage.Width = 300;
            dynamicImage.Height = 200;
            stcdock.Children.Add(dynamicImage);

            WpfDrawingSettings settings = new WpfDrawingSettings();
            settings.IncludeRuntime = true;
            settings.TextAsGeometry = false;
            string svgTestFile = @"\Downloads\acid.svg";
            StreamSvgConverter converter = new StreamSvgConverter(settings);

            MemoryStream memStream = new MemoryStream();
            if (converter.Convert(svgTestFile, memStream))
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.BeginInit();
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.StreamSource = memStream;
                bitmap.EndInit();

                dynamicImage.Source = bitmap;
            }
}

Upvotes: -1

Views: 742

Answers (1)

Kris Meissmer
Kris Meissmer

Reputation: 34

.svg are not nativly supported in wpf.

https://www.nuget.org/packages/svg

This package promisses to add this functionality.

Upvotes: -1

Related Questions