コード例 #1
0
        internal static DrawingGroup SvgFileToWpfObject(string filepath, WpfDrawingSettings wpfDrawingSettings)
        {
            if (wpfDrawingSettings == null) //use defaults if null
            {
                wpfDrawingSettings = new WpfDrawingSettings {
                    IncludeRuntime = false, TextAsGeometry = false, OptimizePath = true
                }
            }
            ;
            var reader = new FileSvgReader(wpfDrawingSettings);

            //this is straight forward, but in this version of the dlls there is an error when name starts with a digit
            //var uri = new Uri(Path.GetFullPath(filepath));
            //reader.Read(uri); //accessing using the filename results is problems with the uri (if the dlls are packed in ressources)
            //return reader.Drawing;

            //this should be faster, but using CreateReader will loose text items like "JOG" ?!
            //using (var stream = File.OpenRead(Path.GetFullPath(filepath)))
            //{
            //    //workaround: error when Id starts with a number
            //    var doc = XDocument.Load(stream);
            //    ReplaceIdsWithNumbers(doc.Root); //id="3d-view-icon" -> id="_3d-view-icon"
            //    using (var xmlReader = doc.CreateReader())
            //    {
            //        reader.Read(xmlReader);
            //        return reader.Drawing;
            //    }
            //}

            filepath = Path.GetFullPath(filepath);
            Stream stream = IsSvgz(filepath)
                ? (Stream) new GZipStream(File.OpenRead(filepath), CompressionMode.Decompress, false)
                : File.OpenRead(filepath);
            var doc = XDocument.Load(stream);

            stream.Dispose();

            //workaround: error when Id starts with a number
            FixIds(doc.Root); //id="3d-view-icon" -> id="_3d-view-icon"
            using (var ms = new MemoryStream())
            {
                doc.Save(ms);
                ms.Position = 0L;
                reader.Read(ms);
                var drawing = reader.Drawing;
                reader.Dispose();

                return(drawing);
            }
        }