Beispiel #1
0
        private void LoadSvg(Item item, Action <string> statusOpen, Action <string> statusToPicture)
        {
            var currentDirectory = Directory.GetCurrentDirectory();

            try
            {
                if (!File.Exists(item.SvgPath))
                {
                    return;
                }

                Directory.SetCurrentDirectory(Path.GetDirectoryName(item.SvgPath));

                var stopwatchOpen = Stopwatch.StartNew();
                item.Svg = SKSvg.Open(item.SvgPath);
                stopwatchOpen.Stop();
                statusOpen?.Invoke($"{Math.Round(stopwatchOpen.Elapsed.TotalMilliseconds, 3)}ms");
                Debug.WriteLine($"Open: {Math.Round(stopwatchOpen.Elapsed.TotalMilliseconds, 3)}ms");

                if (item.Svg != null)
                {
                    var stopwatchToPicture = Stopwatch.StartNew();
                    item.Picture  = SKSvg.ToPicture(item.Svg, out var drawable);
                    item.Drawable = drawable;
                    stopwatchToPicture.Stop();
                    statusToPicture?.Invoke($"{Math.Round(stopwatchToPicture.Elapsed.TotalMilliseconds, 3)}ms");
                    Debug.WriteLine($"ToPicture: {Math.Round(stopwatchToPicture.Elapsed.TotalMilliseconds, 3)}ms");
                }
                else
                {
                    statusToPicture?.Invoke($"");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Failed to load svg file: {item.SvgPath}");
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }

            Directory.SetCurrentDirectory(currentDirectory);
        }
Beispiel #2
0
        public void ExportItem(string svgPath, string outputPath, SKColor background, float scaleX, float scaleY)
        {
            if (!File.Exists(svgPath))
            {
                return;
            }

            var currentDirectory = Directory.GetCurrentDirectory();

            Directory.SetCurrentDirectory(Path.GetDirectoryName(svgPath));

            var extension = Path.GetExtension(outputPath);

            if (string.Compare(extension, ".pdf", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    picture.ToPdf(outputPath, background, scaleX, scaleY);
                }
            }
            else if (string.Compare(extension, ".xps", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    picture.ToXps(outputPath, background, scaleX, scaleY);
                }
            }
            else if (string.Compare(extension, ".svg", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    picture.ToSvg(outputPath, background, scaleX, scaleY);
                }
            }
            else if (string.Compare(extension, ".jpeg", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    using var stream = File.OpenWrite(outputPath);
#if USE_COLORSPACE
                    picture.ToImage(stream, background, SKEncodedImageFormat.Jpeg, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType, SvgPaintingExtensions.Srgb);
#else
                    picture.ToImage(stream, background, SKEncodedImageFormat.Jpeg, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType);
#endif
                }
            }
            else if (string.Compare(extension, ".jpg", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    using var stream = File.OpenWrite(outputPath);
#if USE_COLORSPACE
                    picture.ToImage(stream, background, SKEncodedImageFormat.Jpeg, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType, SvgPaintingExtensions.Srgb);
#else
                    picture.ToImage(stream, background, SKEncodedImageFormat.Jpeg, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType);
#endif
                }
            }
            else if (string.Compare(extension, ".png", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    using var stream = File.OpenWrite(outputPath);
#if USE_COLORSPACE
                    picture.ToImage(stream, background, SKEncodedImageFormat.Png, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType, SvgPaintingExtensions.Srgb);
#else
                    picture.ToImage(stream, background, SKEncodedImageFormat.Png, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType);
#endif
                }
            }
            else if (string.Compare(extension, ".webp", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var svg = SKSvg.Open(svgPath);
                using var picture = SKSvg.ToPicture(svg);
                if (picture != null)
                {
                    using var stream = File.OpenWrite(outputPath);
#if USE_COLORSPACE
                    picture.ToImage(stream, background, SKEncodedImageFormat.Webp, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType, SvgPaintingExtensions.Srgb);
#else
                    picture.ToImage(stream, background, SKEncodedImageFormat.Webp, 100, scaleX, scaleY, SKSvg.s_colorType, SKSvg.s_alphaType);
#endif
                }
            }

            Directory.SetCurrentDirectory(currentDirectory);
        }