Click here to Skip to main content
15,893,904 members
Articles / Desktop Programming / WPF

SharpVectors - SVG# Reloaded: An Introduction

Rate me:
Please Sign up or sign in to vote.
4.98/5 (33 votes)
17 Nov 2010BSD10 min read 205.8K   21.7K   101  
A C# library for converting SVG to WPF and viewing SVG files in WPF Applications
using System;
using System.IO;
using System.Text;
using System.Windows;
using System.Collections.Generic;

using System.Windows.Media;
using System.Windows.Media.Imaging;

using SharpVectors.Dom.Svg;
using SharpVectors.Renderers.Wpf;

using SharpVectors.Runtime;

namespace SharpVectors.Converters
{
    public sealed class EmbeddedImageVisitor : WpfEmbeddedImageVisitor
    {
        public EmbeddedImageVisitor()
        {
        }

        public override BitmapSource Visit(SvgImageElement element, 
            WpfDrawingContext context)
        {
            string sURI    = element.Href.AnimVal;
            int nColon     = sURI.IndexOf(":");
            int nSemiColon = sURI.IndexOf(";");
            int nComma     = sURI.IndexOf(",");

            string sMimeType  = sURI.Substring(nColon + 1, 
                nSemiColon - nColon - 1);

            string sContent   = sURI.Substring(nComma + 1);
            byte[] imageBytes = Convert.FromBase64CharArray(sContent.ToCharArray(),
                0, sContent.Length);

            //BitmapImage imageSource = new BitmapImage();
            //imageSource.BeginInit();
            //imageSource.StreamSource = new MemoryStream(imageBytes);
            //imageSource.EndInit();

            return new EmbeddedBitmapSource(new MemoryStream(imageBytes));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Engineer
Japan Japan
Systems Engineer

Comments and Discussions