Example #1
0
        private bool SaveFile(TextWriter textWriter)
        {
            _writerErrorOccurred = false;

            if (this.UseFrameXamlWriter)
            {
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.Indent             = true;
                writerSettings.OmitXmlDeclaration = true;
                writerSettings.Encoding           = Encoding.UTF8;
                using (XmlWriter writer = XmlWriter.Create(
                           textWriter, writerSettings))
                {
                    System.Windows.Markup.XamlWriter.Save(
                        _drawing, writer);
                }
            }
            else
            {
                try
                {
                    XmlXamlWriter xamlWriter = new XmlXamlWriter(
                        this.DrawingSettings);

                    xamlWriter.Save(_drawing, textWriter);
                }
                catch
                {
                    _writerErrorOccurred = true;

                    if (_fallbackOnWriterError)
                    {
                        XmlWriterSettings writerSettings = new XmlWriterSettings();
                        writerSettings.Indent             = true;
                        writerSettings.OmitXmlDeclaration = true;
                        writerSettings.Encoding           = Encoding.UTF8;
                        using (XmlWriter writer = XmlWriter.Create(
                                   textWriter, writerSettings))
                        {
                            System.Windows.Markup.XamlWriter.Save(
                                _drawing, writer);
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return(true);
        }
 internal static string WpfObjToXaml(object wpfObject, bool includeRuntime)
 {
     XmlXamlWriter writer = new XmlXamlWriter(new WpfDrawingSettings { IncludeRuntime = includeRuntime});
     var xaml = writer.Save(wpfObject);
     return xaml;
 }
Example #3
0
        private bool SaveFile(Drawing drawing, string fileName, string xamlFileName)
        {
            _writerErrorOccurred = false;

            if (String.IsNullOrEmpty(xamlFileName))
            {
                string fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);

                string workingDir = Path.GetDirectoryName(fileName);
                xamlFileName = Path.Combine(workingDir,
                                            fileNameWithoutExt + ".xaml");
            }
            else
            {
                string fileExt = Path.GetExtension(xamlFileName);
                if (String.IsNullOrEmpty(fileExt))
                {
                    xamlFileName += ".xaml";
                }
                else if (!String.Equals(fileExt, ".xaml",
                                        StringComparison.OrdinalIgnoreCase))
                {
                    xamlFileName = Path.ChangeExtension(xamlFileName, ".xaml");
                }
            }

            if (File.Exists(xamlFileName))
            {
                File.SetAttributes(xamlFileName, FileAttributes.Normal);
                File.Delete(xamlFileName);
            }

            if (this.UseFrameXamlWriter)
            {
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.Indent             = true;
                writerSettings.OmitXmlDeclaration = true;
                writerSettings.Encoding           = Encoding.UTF8;
                using (FileStream xamlFile = File.Create(xamlFileName))
                {
                    using (XmlWriter writer = XmlWriter.Create(
                               xamlFile, writerSettings))
                    {
                        System.Windows.Markup.XamlWriter.Save(
                            drawing, writer);
                    }
                }
            }
            else
            {
                try
                {
                    XmlXamlWriter xamlWriter = new XmlXamlWriter(
                        this.DrawingSettings);

                    using (FileStream xamlFile = File.Create(xamlFileName))
                    {
                        xamlWriter.Save(drawing, xamlFile);
                    }
                }
                catch
                {
                    _writerErrorOccurred = true;

                    if (_fallbackOnWriterError)
                    {
                        if (File.Exists(xamlFileName))
                        {
                            File.Move(xamlFileName, xamlFileName + ".bak");
                        }

                        XmlWriterSettings writerSettings = new XmlWriterSettings();
                        writerSettings.Indent             = true;
                        writerSettings.OmitXmlDeclaration = true;
                        writerSettings.Encoding           = Encoding.UTF8;
                        using (FileStream xamlFile = File.Create(xamlFileName))
                        {
                            using (XmlWriter writer = XmlWriter.Create(
                                       xamlFile, writerSettings))
                            {
                                System.Windows.Markup.XamlWriter.Save(
                                    drawing, writer);
                            }
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (this.SaveZaml)
            {
                string zamlFileName = Path.ChangeExtension(xamlFileName, ".zaml");

                if (File.Exists(zamlFileName))
                {
                    File.SetAttributes(zamlFileName, FileAttributes.Normal);
                    File.Delete(zamlFileName);
                }

                FileStream zamlSourceFile = new FileStream(xamlFileName, FileMode.Open,
                                                           FileAccess.Read, FileShare.Read);
                byte[] buffer = new byte[zamlSourceFile.Length];
                // Read the file to ensure it is readable.
                int count = zamlSourceFile.Read(buffer, 0, buffer.Length);
                if (count != buffer.Length)
                {
                    zamlSourceFile.Close();
                    return(false);
                }
                zamlSourceFile.Close();

                FileStream zamlDestFile = File.Create(zamlFileName);

                GZipStream zipStream = new GZipStream(zamlDestFile, CompressionMode.Compress, true);
                zipStream.Write(buffer, 0, buffer.Length);

                zipStream.Close();

                zamlDestFile.Close();

                _zamlFile = zamlFileName;
            }
            _xamlFile = xamlFileName;

            if (!this.SaveXaml && File.Exists(xamlFileName))
            {
                File.Delete(xamlFileName);
                _xamlFile = null;
            }

            return(true);
        }
        public static string Convert(object obj)
        {
            XmlXamlWriter writer = new XmlXamlWriter();

            return(writer.Save(obj));
        }
        private bool SaveFile(string fileName)
        {
            if (_workingDir == null || (!this.SaveXaml && !this.SaveZaml))
            {
                return(false);
            }

            _writerErrorOccurred = false;

            string fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);

            string xamlFileName = Path.Combine(_workingDir.FullName, fileNameWithoutExt + XamlExt);

            if (File.Exists(xamlFileName))
            {
                File.SetAttributes(xamlFileName, FileAttributes.Normal);
                File.Delete(xamlFileName);
            }

            if (this.UseFrameXamlWriter)
            {
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.Indent             = true;
                writerSettings.Encoding           = Encoding.UTF8;
                writerSettings.OmitXmlDeclaration = true;

                using (FileStream xamlFile = File.Create(xamlFileName))
                {
                    using (XmlWriter writer = XmlWriter.Create(xamlFile, writerSettings))
                    {
                        System.Windows.Markup.XamlWriter.Save(_drawing, writer);
                    }
                }
            }
            else
            {
                try
                {
                    XmlXamlWriter xamlWriter = new XmlXamlWriter(this.DrawingSettings);

                    using (FileStream xamlFile = File.Create(xamlFileName))
                    {
                        xamlWriter.Save(_drawing, xamlFile);
                    }
                }
                catch
                {
                    _writerErrorOccurred = true;

                    if (_fallbackOnWriterError)
                    {
                        // If the file exist, we back it up and save a new file...
                        if (File.Exists(xamlFileName))
                        {
                            File.Move(xamlFileName, xamlFileName + BackupExt);
                        }

                        XmlWriterSettings writerSettings = new XmlWriterSettings();
                        writerSettings.Indent             = true;
                        writerSettings.Encoding           = Encoding.UTF8;
                        writerSettings.OmitXmlDeclaration = true;

                        using (FileStream xamlFile = File.Create(xamlFileName))
                        {
                            using (XmlWriter writer = XmlWriter.Create(xamlFile, writerSettings))
                            {
                                System.Windows.Markup.XamlWriter.Save(_drawing, writer);
                            }
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (this.SaveZaml)
            {
                string zamlFileName = Path.ChangeExtension(xamlFileName, CompressedXamlExt);

                if (File.Exists(zamlFileName))
                {
                    File.SetAttributes(zamlFileName, FileAttributes.Normal);
                    File.Delete(zamlFileName);
                }

                FileStream zamlSourceFile = new FileStream(xamlFileName,
                                                           FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] buffer = new byte[zamlSourceFile.Length];
                // Read the file to ensure it is readable.
                int count = zamlSourceFile.Read(buffer, 0, buffer.Length);
                if (count != buffer.Length)
                {
                    zamlSourceFile.Close();
                    return(false);
                }
                zamlSourceFile.Close();

                FileStream zamlDestFile = File.Create(zamlFileName);

                var zipStream = new GZipStream(zamlDestFile, CompressionMode.Compress, true);
                zipStream.Write(buffer, 0, buffer.Length);

                zipStream.Close();

                zamlDestFile.Close();

                _zamlFile = zamlFileName;
            }
            _xamlFile = xamlFileName;

            if (!this.SaveXaml && File.Exists(xamlFileName))
            {
                File.Delete(xamlFileName);
                _xamlFile = null;
            }

            return(true);
        }
        public static string Convert(object obj)
        {
            XmlXamlWriter writer = new XmlXamlWriter();

            return writer.Save(obj);
        }
Example #7
0
 public void Handwheel2() //integrated conversion, manual writing
 {
     var drawing = SvgConverter.ConverterLogic.SvgFileToWpfObject("TestFiles\\Handwheel.svg", null);
     XmlXamlWriter writer = new XmlXamlWriter(null);
     var xaml = writer.Save(drawing);
     Console.WriteLine(xaml);
     Clipboard.SetText(xaml);
 }
Example #8
0
        private bool SaveXamlFile(Drawing drawing, string fileName, string imageFileName)
        {
            _writerErrorOccurred = false;

            string xamlFileName = null;
            if (String.IsNullOrEmpty(imageFileName))
            {
                string fileNameWithoutExt =
                    Path.GetFileNameWithoutExtension(fileName);

                string workingDir = Path.GetDirectoryName(fileName);
                xamlFileName = Path.Combine(workingDir,
                    fileNameWithoutExt + ".xaml");
            }
            else
            {
                string fileExt = Path.GetExtension(imageFileName);
                if (String.IsNullOrEmpty(fileExt))
                {
                    xamlFileName = imageFileName + ".xaml";
                }
                else if (!String.Equals(fileExt, ".xaml",
                    StringComparison.OrdinalIgnoreCase))
                {
                    xamlFileName = Path.ChangeExtension(imageFileName, ".xaml");
                }
            }

            if (File.Exists(xamlFileName))
            {
                File.SetAttributes(xamlFileName, FileAttributes.Normal);
                File.Delete(xamlFileName);
            }

            if (this.UseFrameXamlWriter)
            {
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.Indent = true;
                writerSettings.OmitXmlDeclaration = true;
                writerSettings.Encoding = Encoding.UTF8;
                using (FileStream xamlFile = File.Create(xamlFileName))
                {
                    using (XmlWriter writer = XmlWriter.Create(
                        xamlFile, writerSettings))
                    {
                        System.Windows.Markup.XamlWriter.Save(
                            drawing, writer);
                    }
                }
            }
            else
            {
                try
                {
                    XmlXamlWriter xamlWriter = new XmlXamlWriter(
                        this.DrawingSettings);

                    using (FileStream xamlFile = File.Create(xamlFileName))
                    {
                        xamlWriter.Save(drawing, xamlFile);
                    }
                }
                catch
                {
                    _writerErrorOccurred = true;

                    if (_fallbackOnWriterError)
                    {
                        if (File.Exists(xamlFileName))
                        {
                            File.Move(xamlFileName, xamlFileName + ".bak");
                        }

                        XmlWriterSettings writerSettings = new XmlWriterSettings();
                        writerSettings.Indent = true;
                        writerSettings.OmitXmlDeclaration = true;
                        writerSettings.Encoding = Encoding.UTF8;
                        using (FileStream xamlFile = File.Create(xamlFileName))
                        {
                            using (XmlWriter writer = XmlWriter.Create(
                                xamlFile, writerSettings))
                            {
                                System.Windows.Markup.XamlWriter.Save(
                                    drawing, writer);
                            }
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (this.SaveZaml)
            {
                string zamlFileName = Path.ChangeExtension(xamlFileName, ".zaml");

                if (File.Exists(zamlFileName))
                {
                    File.SetAttributes(zamlFileName, FileAttributes.Normal);
                    File.Delete(zamlFileName);
                }

                FileStream zamlSourceFile = new FileStream(xamlFileName, FileMode.Open,
                    FileAccess.Read, FileShare.Read);
                byte[] buffer = new byte[zamlSourceFile.Length];
                // Read the file to ensure it is readable.
                int count = zamlSourceFile.Read(buffer, 0, buffer.Length);
                if (count != buffer.Length)
                {
                    zamlSourceFile.Close();
                    return false;
                }
                zamlSourceFile.Close();

                FileStream zamlDestFile = File.Create(zamlFileName);

                GZipStream zipStream = new GZipStream(zamlDestFile, CompressionMode.Compress, true);
                zipStream.Write(buffer, 0, buffer.Length);

                zipStream.Close();

                zamlDestFile.Close();
            }

            if (!this.SaveXaml && File.Exists(xamlFileName))
            {
                File.Delete(xamlFileName);
            }

            return true;
        }
Example #9
0
 public void Handwheel1() //pure svg# without any modifications
 {
     var fileReader = new FileSvgReader(null);
     DrawingGroup drawing = fileReader.Read("TestFiles\\Handwheel.svg");
     XmlXamlWriter writer = new XmlXamlWriter(null);
     var xaml = writer.Save(drawing);
     Console.WriteLine(xaml);
     Clipboard.SetText(xaml);
 }
        private bool SaveFile(string fileName)
        {
            if (_workingDir == null || (!this.SaveXaml && !this.SaveZaml))
            {
                return false;
            }

            _writerErrorOccurred = false;

            string fileNameWithoutExt = Path.GetFileNameWithoutExtension(fileName);

            string xamlFileName = Path.Combine(_workingDir.FullName,
                fileNameWithoutExt + ".xaml");

            if (File.Exists(xamlFileName))
            {
                File.SetAttributes(xamlFileName, FileAttributes.Normal);
                File.Delete(xamlFileName);
            }

            if (this.UseFrameXamlWriter)
            {
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.Indent = true;
                writerSettings.OmitXmlDeclaration = true;
                writerSettings.Encoding = Encoding.UTF8;
                using (FileStream xamlFile = File.Create(xamlFileName))
                {
                    using (XmlWriter writer = XmlWriter.Create(
                        xamlFile, writerSettings))
                    {
                        System.Windows.Markup.XamlWriter.Save(
                            _drawing, writer);
                    }
                }
            }
            else
            {
                try
                {
                    XmlXamlWriter xamlWriter = new XmlXamlWriter(
                        this.DrawingSettings);

                    using (FileStream xamlFile = File.Create(xamlFileName))
                    {
                        xamlWriter.Save(_drawing, xamlFile);
                    }
                }
                catch
                {
                    _writerErrorOccurred = true;

                    if (_fallbackOnWriterError)
                    {
                        // If the file exist, we back it up and save a new file...
                        if (File.Exists(xamlFileName))
                        {
                            File.Move(xamlFileName, xamlFileName + ".bak");
                        }

                        XmlWriterSettings writerSettings = new XmlWriterSettings();
                        writerSettings.Indent = true;
                        writerSettings.OmitXmlDeclaration = true;
                        writerSettings.Encoding = Encoding.UTF8;
                        using (FileStream xamlFile = File.Create(xamlFileName))
                        {
                            using (XmlWriter writer = XmlWriter.Create(
                                xamlFile, writerSettings))
                            {
                                System.Windows.Markup.XamlWriter.Save(
                                    _drawing, writer);
                            }
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            if (this.SaveZaml)
            {
                string zamlFileName = Path.ChangeExtension(xamlFileName, ".zaml");

                if (File.Exists(zamlFileName))
                {
                    File.SetAttributes(zamlFileName, FileAttributes.Normal);
                    File.Delete(zamlFileName);
                }

                FileStream zamlSourceFile = new FileStream(
                    xamlFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                byte[] buffer = new byte[zamlSourceFile.Length];
                // Read the file to ensure it is readable.
                int count = zamlSourceFile.Read(buffer, 0, buffer.Length);
                if (count != buffer.Length)
                {
                    zamlSourceFile.Close();
                    return false;
                }
                zamlSourceFile.Close();

                FileStream zamlDestFile = File.Create(zamlFileName);

                GZipStream zipStream = new GZipStream(zamlDestFile,
                    CompressionMode.Compress, true);
                zipStream.Write(buffer, 0, buffer.Length);

                zipStream.Close();

                zamlDestFile.Close();

                _zamlFile = zamlFileName;
            }
            _xamlFile = xamlFileName;

            if (!this.SaveXaml && File.Exists(xamlFileName))
            {
                File.Delete(xamlFileName);
                _xamlFile = null;
            }

            return true;
        }
        private bool SaveFile(TextWriter textWriter)
        {
            _writerErrorOccurred = false;

            if (this.UseFrameXamlWriter)
            {
                XmlWriterSettings writerSettings = new XmlWriterSettings();
                writerSettings.Indent = true;
                writerSettings.OmitXmlDeclaration = true;
                writerSettings.Encoding = Encoding.UTF8;
                using (XmlWriter writer = XmlWriter.Create(
                    textWriter, writerSettings))
                {
                    System.Windows.Markup.XamlWriter.Save(
                        _drawing, writer);
                }
            }
            else
            {
                try
                {
                    XmlXamlWriter xamlWriter = new XmlXamlWriter(
                        this.DrawingSettings);

                    xamlWriter.Save(_drawing, textWriter);
                }
                catch
                {
                    _writerErrorOccurred = true;

                    if (_fallbackOnWriterError)
                    {
                        XmlWriterSettings writerSettings = new XmlWriterSettings();
                        writerSettings.Indent = true;
                        writerSettings.OmitXmlDeclaration = true;
                        writerSettings.Encoding = Encoding.UTF8;
                        using (XmlWriter writer = XmlWriter.Create(
                            textWriter, writerSettings))
                        {
                            System.Windows.Markup.XamlWriter.Save(
                                _drawing, writer);
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            return true;
        }
Example #12
0
 public void Handwheel1()
 {
     var fileReader = new FileSvgReader(null);
     DrawingGroup drawing = fileReader.Read("..\\..\\TestFiles\\Handwheel.svg");
     XmlXamlWriter writer = new XmlXamlWriter(null);
     var xaml = writer.Save(drawing);
     Console.WriteLine(xaml);
     Clipboard.SetText(xaml);
 }
Example #13
0
 public void Handwheel2()
 {
     var drawing = SvgConverter.ConverterLogic.SvgFileToWpfObject("..\\..\\TestFiles\\Handwheel.svg", null);
     XmlXamlWriter writer = new XmlXamlWriter(null);
     var xaml = writer.Save(drawing);
     Console.WriteLine(xaml);
     Clipboard.SetText(xaml);
 }