예제 #1
0
        public static bool Save(FileInfo path, DirectoryInfo?output, string format, int quality, string background, float scale, float scaleX, float scaleY, bool quiet, int i)
        {
            try
            {
                if (quiet == false)
                {
                    Log($"[{i}] File: {path}");
                }

                var    extension = path.Extension;
                string imagePath = path.FullName.Remove(path.FullName.Length - extension.Length) + "." + format.ToLower();
                if (output != null && !string.IsNullOrEmpty(output.FullName))
                {
                    imagePath = Path.Combine(output.FullName, Path.GetFileName(imagePath));
                }

                Directory.SetCurrentDirectory(Path.GetDirectoryName(path.FullName));

                using (var svg = new SKSvg())
                {
                    if (svg.Load(path.FullName) != null)
                    {
                        if (Enum.TryParse <SKEncodedImageFormat>(format, true, out var skEncodedImageFormat))
                        {
                            if (SKColor.TryParse(background, out var skBackgroundColor))
                            {
                                if (scale != 1f)
                                {
                                    svg.Save(imagePath, skBackgroundColor, skEncodedImageFormat, quality, scale, scale);
                                }
                                else
                                {
                                    svg.Save(imagePath, skBackgroundColor, skEncodedImageFormat, quality, scaleX, scaleY);
                                }
                            }
                            else
                            {
                                throw new ArgumentException($"Invalid output image background.", nameof(background));
                            }
                        }
                        else
                        {
                            throw new ArgumentException($"Invalid output image format.", nameof(format));
                        }
                    }
                }

                if (quiet == false)
                {
                    Log($"[{i}] Success: {imagePath}");
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (quiet == false)
                {
                    Log($"[{i}] Error: {path}");
                    Error(ex);
                }
            }

            return(false);
        }
예제 #2
0
        static bool Save(FileInfo inputPath, string outputPath, string format, int quality, string background, float scale, float scaleX, float scaleY, bool quiet, int i)
        {
            if (quiet == false)
            {
                Log($"[{i}] File: {inputPath}");
            }

            using var svg = new SKSvg();

            if (svg.Load(inputPath.FullName) == null)
            {
                Log($"Error: Failed to load input file: {inputPath.FullName}");
                return(false);
            }

            if (string.Compare(format, "pdf", StringComparison.OrdinalIgnoreCase) == 0)
            {
                if (SKColor.TryParse(background, out var skBackgroundColor))
                {
                    if (scale != 1f)
                    {
                        svg.Picture?.ToPdf(outputPath, skBackgroundColor, scale, scale);
                    }
                    else
                    {
                        svg.Picture?.ToPdf(outputPath, skBackgroundColor, scaleX, scaleY);
                    }
                }
                else
                {
                    throw new ArgumentException($"Invalid output image background.", nameof(background));
                }
            }
            else if (string.Compare(format, "xps", StringComparison.OrdinalIgnoreCase) == 0)
            {
                if (SKColor.TryParse(background, out var skBackgroundColor))
                {
                    if (scale != 1f)
                    {
                        svg.Picture?.ToXps(outputPath, skBackgroundColor, scale, scale);
                    }
                    else
                    {
                        svg.Picture?.ToXps(outputPath, skBackgroundColor, scaleX, scaleY);
                    }
                }
                else
                {
                    throw new ArgumentException($"Invalid output image background.", nameof(background));
                }
            }
            else
            {
                if (Enum.TryParse <SKEncodedImageFormat>(format, true, out var skEncodedImageFormat))
                {
                    if (SKColor.TryParse(background, out var skBackgroundColor))
                    {
                        if (scale != 1f)
                        {
                            svg.Save(outputPath, skBackgroundColor, skEncodedImageFormat, quality, scale, scale);
                        }
                        else
                        {
                            svg.Save(outputPath, skBackgroundColor, skEncodedImageFormat, quality, scaleX, scaleY);
                        }
                    }
                    else
                    {
                        throw new ArgumentException($"Invalid output image background.", nameof(background));
                    }
                }
                else
                {
                    throw new ArgumentException($"Invalid output image format.", nameof(format));
                }
            }

            if (quiet == false)
            {
                Log($"[{i}] Success: {outputPath}");
            }

            return(true);
        }