Esempio n. 1
0
        public static void ConvertSvg(string filename, string destfile)
        {
            var settings = new WpfDrawingSettings();

            settings.IncludeRuntime = false;
            var converter = new FileSvgConverter(settings);

            converter.Convert(filename, destfile);
        }
        //--------------------------------------------------------------------------------------------------

        public override bool Execute()
        {
            bool result = true;

            if (Inputs.Length != Outputs.Length)
            {
                Log.LogError("The number of source items is not equal to the number of target items.");
                return(false);
            }

            // Prepare converter
            WpfDrawingSettings settings = new WpfDrawingSettings();

            settings.IncludeRuntime = false;
            settings.TextAsGeometry = true;
            settings.OptimizePath   = true;
            var converter = new FileSvgConverter(true, false, settings);

            // Iterate through files
            for (var i = 0; i < Inputs.Length; i++)
            {
                ITaskItem sourceItem = Inputs[i];
                try
                {
                    if (sourceItem.ItemSpec.Length == 0)
                    {
                        continue;
                    }

                    ITaskItem destItem = Outputs[i];
                    if (destItem.ItemSpec.Length == 0)
                    {
                        Log.LogError($"The target item to the source item {sourceItem.ItemSpec} is empty.");
                        result = false;
                        continue;
                    }

                    if (!converter.Convert(sourceItem.ItemSpec, destItem.ItemSpec))
                    {
                        Log.LogError($"The file {sourceItem.ItemSpec} could not be converted to XAML.");
                        result = false;
                    }
                }
                catch (Exception e)
                {
                    Log.LogError($"The conversion of the file {sourceItem.ItemSpec} raised an exception: {e.Message}");
                }
            }

            return(result);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            // 1. Create conversion options
            WpfDrawingSettings settings = new WpfDrawingSettings();

            settings.IncludeRuntime = false;
            settings.TextAsGeometry = true;

            // 2. Select a file to be converted
            string svgTestFile = "Test.svg";

            // 3. Create a file converter
            FileSvgConverter converter = new FileSvgConverter(settings);

            // 4. Perform the conversion to XAML
            converter.Convert(svgTestFile);
        }
        public void ImportSvg()
        {
            // Select a XML file
            var dialog = new VistaOpenFileDialog {
                Filter = "Scalable Vector Graphics (*.svg)|*.svg"
            };

            var result = dialog.ShowDialog();

            if (result == false)
            {
                return;
            }

            var fileName     = dialog.FileName;
            var xamlFileName = fileName.Replace(".svg", ".xaml");
            var settings     = new WpfDrawingSettings {
                IncludeRuntime = true, TextAsGeometry = true
            };
            var converter = new FileSvgConverter(settings);

            using var fileStream = File.OpenRead(fileName);
            converter.Convert(fileStream, xamlFileName);
            var xaml = File.ReadAllText(xamlFileName);

            File.Delete(xamlFileName);

            var parsed = (DrawingGroup)XamlReader.Parse(xaml, new ParserContext {
                BaseUri = new Uri(Path.GetDirectoryName(fileName))
            });
            var geometry = GatherGeometry(parsed);

            var group = new DrawingGroup {
                Children = new DrawingCollection(geometry)
            };
            var stringValue = "";

            foreach (var geometryDrawing in geometry)
            {
                var scaled = Geometry.Combine(
                    geometryDrawing.Geometry,
                    geometryDrawing.Geometry, GeometryCombineMode.Intersect,
                    new TransformGroup
                {
                    Children = new TransformCollection
                    {
                        new TranslateTransform(group.Bounds.X * -1, group.Bounds.Y * -1),
                        new ScaleTransform(1.0 / group.Bounds.Width, 1.0 / group.Bounds.Height)
                    }
                }
                    );

                var scaledString = scaled.ToString(CultureInfo.InvariantCulture)
                                   .Replace("F1", "")
                                   .Replace(";", ",")
                                   .Replace("L", " L")
                                   .Replace("C", " C");

                stringValue = stringValue + " " + scaledString;
            }

            InputShapeData = stringValue.Trim();
            ApplyInput();
        }