Example #1
0
        public override void Resize(string sourceFile, string destinationFile, ImageAsset asset, OutputConfig outputConfig)
        {
            int    sourceNominalWidth  = asset.Width;
            int    sourceNominalHeight = asset.Height;
            double resizeRatio         = outputConfig.Ratio;

            var bmp = SKBitmap.Decode(sourceFile);

            var sourceActualWidth  = bmp.Width;
            var sourceActualHeight = bmp.Height;

            var nominalRatio = Math.Max((double)sourceNominalWidth / (double)sourceActualWidth, (double)sourceNominalHeight / (double)sourceActualHeight);

            var adjustRatio = nominalRatio * resizeRatio;

            var resizeMethod = GetSKBitmapResizeMethod(outputConfig.BitmapResizeMethod ?? BitmapResizeMethod.Box);

            var newBmp = bmp.Resize(new SKImageInfo((int)(bmp.Width * adjustRatio), (int)(bmp.Height * adjustRatio)), resizeMethod);

            var img  = SKImage.FromBitmap(newBmp);
            var data = img.Encode(SKImageEncodeFormat.Png, 100);

            using (var fs = File.Open(destinationFile, FileMode.Create))
                data.SaveTo(fs);
        }
Example #2
0
        List <string> RunExternalCommand(Config config, ImageAsset asset, OutputConfig outputConfig, FileInfo outputFileInfo, string command)
        {
            var output = new List <string>();

            int width  = (int)((double)asset.Width * outputConfig.Ratio);
            int height = (int)((double)asset.Width * outputConfig.Ratio);

            command = command.Replace("{outputFile}", outputFileInfo.FullName);
            command = command.Replace("{width}", width.ToString());
            command = command.Replace("{height}", height.ToString());

            var args = ArgumentsParser.GetArguments(command);

            if (!args?.Any() ?? true)
            {
                return(output);
            }

            var file = args[0];

            args.RemoveAt(0);

            var argStr = string.Join(" ", args);

            var process = new System.Diagnostics.Process();

            process.StartInfo = new System.Diagnostics.ProcessStartInfo(file, argStr);
            process.StartInfo.UseShellExecute        = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError  = true;
            process.OutputDataReceived += (sender, e) =>
            {
                output.Add(e.Data);
            };
            process.ErrorDataReceived += (sender, e) =>
            {
                output.Add(e.Data);
            };
            process.Start();
            process.WaitForExit();

            return(output);
        }
Example #3
0
        public override void Resize(string sourceFile, string destinationFile, ImageAsset asset, OutputConfig outputConfig)
        {
            int    sourceNominalWidth  = asset.Width;
            int    sourceNominalHeight = asset.Height;
            double resizeRatio         = outputConfig.Ratio;

            var fillColor = asset.FillColor;

            if (string.IsNullOrEmpty(fillColor))
            {
                fillColor = outputConfig.FillColor;
            }

            // For SVG's we can optionally change the fill color on all paths
            if (!string.IsNullOrEmpty(fillColor))
            {
                var svgText = File.ReadAllText(sourceFile);

                foreach (var rxPattern in rxFillPatterns)
                {
                    var matches = Regex.Matches(svgText, rxPattern);

                    foreach (Match match in matches)
                    {
                        var fillGroup = match.Groups?["fill"];

                        if (fillGroup != null)
                        {
                            // Replace the matched rx group with our override fill color
                            var a = svgText.Substring(0, fillGroup.Index);
                            var b = svgText.Substring(fillGroup.Index + fillGroup.Length);
                            svgText = a + outputConfig.FillColor.TrimEnd(';') + ";" + b;
                        }
                    }
                }

                // Write our changes out to a temp file so we don't alter the original
                var tempFile = Path.GetTempFileName();
                File.WriteAllText(tempFile, svgText);
                sourceFile = tempFile;
            }

            var svg = new SkiaSharp.Extended.Svg.SKSvg();

            svg.Load(sourceFile);

            // Find the actual size of the SVG
            var sourceActualWidth  = svg.Picture.CullRect.Width;
            var sourceActualHeight = svg.Picture.CullRect.Height;

            // Figure out what the ratio to convert the actual image size to the nominal size is
            var nominalRatio = Math.Max((double)sourceNominalWidth / (double)sourceActualWidth, (double)sourceNominalHeight / (double)sourceActualHeight);

            // Multiply nominal ratio by the resize ratio to get our final ratio we actually adjust by
            var adjustRatio = nominalRatio * resizeRatio;

            // Figure out our scaled width and height to make a new canvas for
            var scaledWidth  = sourceActualWidth * adjustRatio;
            var scaledHeight = sourceActualHeight * adjustRatio;

            // Make a canvas of the target size to draw the svg onto
            var bmp    = new SKBitmap((int)scaledWidth, (int)scaledHeight);
            var canvas = new SKCanvas(bmp);

            // Make a matrix to scale the SVG
            var matrix = SKMatrix.MakeScale((float)adjustRatio, (float)adjustRatio);

            canvas.Clear(SKColors.Transparent);

            // Draw the svg onto the canvas with our scaled matrix
            canvas.DrawPicture(svg.Picture, ref matrix);

            // Save the op
            canvas.Save();

            // Export the canvas
            var img  = SKImage.FromBitmap(bmp);
            var data = img.Encode(SKEncodedImageFormat.Png, 100);

            using (var fs = File.Open(destinationFile, FileMode.Create)) {
                data.SaveTo(fs);
            }
        }
 public abstract void Resize(string sourceFile, string destinationFile, ImageAsset asset, OutputConfig outputConfig);