Click here to Skip to main content
15,879,535 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 204K   21.7K   101  
A C# library for converting SVG to WPF and viewing SVG files in WPF Applications
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using SharpVectors.Dom.Svg;
using SharpVectors.Renderers.Gdi;
using SharpVectors.Net;
using SharpVectors.Dom.Events;

namespace TestSvgControl
{
    public partial class MainForm : Form
    {
        private string _titleBase;

        public MainForm()
        {
            InitializeComponent();

            _titleBase = this.Text;

            Rectangle rectScreen = Screen.PrimaryScreen.WorkingArea;

            this.Width  = (int)(rectScreen.Width * 0.80f);
            this.Height = (int)(rectScreen.Height * 0.90f);
        }

        private void OnDragDrop(object sender, DragEventArgs de)
        {
            string fileName = "";
            if (de.Data.GetDataPresent(DataFormats.Text))
            {
                fileName = (string)de.Data.GetData(DataFormats.Text);
            }
            else if (de.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] fileNames;
                fileNames = (string[])de.Data.GetData(DataFormats.FileDrop);
                fileName = fileNames[0];
            }

            if (!String.IsNullOrEmpty(fileName))
            {
                this.OpenSvgFile(fileName);
            }
        }

        private void OnDragEnter(object sender, DragEventArgs de)
        {
            if (de.Data.GetDataPresent(DataFormats.Text) ||
               de.Data.GetDataPresent(DataFormats.FileDrop))
            {
                de.Effect = DragDropEffects.Copy;
            }
            else
            {
                de.Effect = DragDropEffects.None;
            }
        }

        private void OpenSvgFile(string fileName)
        {
            if (String.IsNullOrEmpty(fileName) || !File.Exists(fileName))
            {
                return;
            }
            string fileExt = Path.GetExtension(fileName);
            if (String.IsNullOrEmpty(fileExt))
            {
                return;
            }
            if (!String.Equals(fileExt, ".svgz", StringComparison.OrdinalIgnoreCase) &&
                !String.Equals(fileExt, ".svg", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            try
            {
                this.Cursor = Cursors.WaitCursor;
                svgPictureBox.SourceURL = fileName;

                // JR Test event
                ISvgDocument doc = svgPictureBox.Window.Document;                
                doc.RootElement.AddEventListener("click", new EventListener(OnSvgElementClicked), false);

                this.Text = _titleBase + " - " + Path.GetFileName(fileName);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        public void OnSvgElementClicked(IEvent e)
        {
            SvgElement el = ((SvgElement)e.Target);
            
            //MessageBox.Show(String.Format("Clicked - LocalName = {0}, ID = {1}", el.LocalName, el.Id));

            MessageBox.Show(el.OuterXml);
        }
    }
}

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