SvgViewer.cs 1.96 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Svg;
using Svg.Transforms;
Tebjan Halm's avatar
Tebjan Halm committed
11
12
using System.Xml;
using System.IO;
13
14
15
16
17
18
19
20
21
22
23
24
25
26

namespace SVGViewer
{
    public partial class SVGViewer : Form
    {
        public SVGViewer()
        {
            InitializeComponent();
        }

        private void open_Click(object sender, EventArgs e)
        {
            if (openSvgFile.ShowDialog() == DialogResult.OK)
            {
C Moore's avatar
C Moore committed
27
28
29
30
31
32
33
34
35
36
37
38
                SvgDocument svgDoc = SvgDocument.Open(openSvgFile.FileName);

                DrawDoc(svgDoc);
            }
        }

        private void ResizeDoc(SvgDocument document)
        {
            if (document.Height > svgImage.Image.Height)
            {
                document.Width = (int)(((double)document.Width / (double)document.Height) * (double)svgImage.Image.Height);
                document.Height = svgImage.Image.Height;
39
40
            }
        }
Tebjan Halm's avatar
Tebjan Halm committed
41

C Moore's avatar
C Moore committed
42
43
44
45
46
        private void DrawDoc(SvgDocument svgDoc)
        {
            ResizeDoc(svgDoc);
            svgImage.Image = svgDoc.Draw();
        }
Tebjan Halm's avatar
Tebjan Halm committed
47
48
49

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
C Moore's avatar
C Moore committed
50
51
            if (string.IsNullOrEmpty(textBox1.Text))
                return;
Tebjan Halm's avatar
Tebjan Halm committed
52

C Moore's avatar
C Moore committed
53
54
55
56
57
            // Need to now disable the DTD settings 
            // (seems to throw an exception, be slow at .NET 4 if you don't)
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.XmlResolver = null;
            settings.DtdProcessing = DtdProcessing.Parse;
Tebjan Halm's avatar
Tebjan Halm committed
58

C Moore's avatar
C Moore committed
59
60
61
62
63
64
65
            XmlReader xmlReader = XmlReader.Create(new StringReader(textBox1.Text), 
                settings);

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlReader);

            SvgDocument svgDoc = SvgDocument.Open(xmlDoc);
Tebjan Halm's avatar
Tebjan Halm committed
66

C Moore's avatar
C Moore committed
67
            DrawDoc(svgDoc);
Tebjan Halm's avatar
Tebjan Halm committed
68
        }
69
70
    }
}