SvgViewer.cs 2.42 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

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

        private void open_Click(object sender, EventArgs e)
        {
H1Gdev's avatar
H1Gdev committed
25
26
27
28
29
30
            try
            {
                if (openSvgFile.ShowDialog() == DialogResult.OK)
                {
                    SvgDocument svgDoc = SvgDocument.Open(openSvgFile.FileName);
                    RenderSvg(svgDoc);
H1Gdev's avatar
H1Gdev committed
31
32
33
34
35

                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.XmlResolver = null;
                    xmlDoc.Load(openSvgFile.FileName);
                    textBox1.Text = xmlDoc.InnerXml;
H1Gdev's avatar
H1Gdev committed
36
37
38
                }
            }
            catch
39
40
            {
            }
41
        }
Tebjan Halm's avatar
Tebjan Halm committed
42
43
44

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
H1Gdev's avatar
H1Gdev committed
45
46
47
48
49
50
51
52
53
54
55
            try
            {
                using (var s = new MemoryStream(Encoding.UTF8.GetBytes(textBox1.Text)))
                {
                    SvgDocument svgDoc = SvgDocument.Open<SvgDocument>(s, null);
                    RenderSvg(svgDoc);
                }
            }
            catch
            {
            }
tebjan's avatar
tebjan committed
56
        }
H1Gdev's avatar
H1Gdev committed
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Control && e.KeyCode == Keys.A)
                {
                    (sender as TextBox).SelectAll();
                }
            }
            catch
            {
            }
        }

tebjan's avatar
tebjan committed
72
73
        private void RenderSvg(SvgDocument svgDoc)
        {
Eric Domke's avatar
Eric Domke committed
74
75
            //var render = new DebugRenderer();
            //svgDoc.Draw(render);
Tebjan Halm's avatar
Tebjan Halm committed
76
            svgImage.Image = svgDoc.Draw();
H1Gdev's avatar
H1Gdev committed
77

78
79
80
81
82
83
            string outputDir;
            if (svgDoc.BaseUri == null)
                outputDir = System.IO.Path.GetDirectoryName(Application.ExecutablePath); 
            else
                outputDir = System.IO.Path.GetDirectoryName(svgDoc.BaseUri.LocalPath);
            svgImage.Image.Save(System.IO.Path.Combine(outputDir, "output.png"));
mrbean-bremen's avatar
mrbean-bremen committed
84
            // svgDoc.Write(System.IO.Path.Combine(outputDir, "output.svg"));
Tebjan Halm's avatar
Tebjan Halm committed
85
        }
86
87
    }
}