Esempio n. 1
0
        internal void ProcessImage(OutputImage outputImage)
        {
            Log.LogMessage($"Generating file '{outputImage.OutputFile}");
            var fi = new FileInfo(outputImage.OutputFile);

            Directory.CreateDirectory(Path.Combine(Build.IntermediateOutputPath, fi.DirectoryName));

            using var image = ImageBase.Load(outputImage.InputFile);

            try
            {
                var context = CreateContext(
                    GetBackgroundColor(outputImage.BackgroundColor, outputImage.RequiresBackgroundColor && image.HasTransparentBackground),
                    Log,
                    1.0,
                    outputImage.Scale,
                    outputImage.Width,
                    outputImage.Height,
                    image.GetOriginalSize());

                if (!context.Scale.X.IsEqualTo(context.Scale.Y))
                {
                    Log.LogWarning("Image aspect ratio is not being maintained.");
                }

                using var tempBitmap = new SKBitmap(context.Size.Width, context.Size.Height);
                using var canvas     = new SKCanvas(tempBitmap);

                canvas.Clear(context.BackgroundColor);
                canvas.Save();
                canvas.Scale(context.Scale.X, context.Scale.Y);

                image.Draw(canvas, context);

                ApplyWatermark(outputImage, context, Log, canvas);

                using var outputBitmap = ApplyPadding(outputImage, image, context, tempBitmap);

                using var stream = File.Create(outputImage.OutputFile);
                outputBitmap.Encode(stream, SKEncodedImageFormat.Png, 100);
            }
            catch (System.Exception ex)
            {
#if DEBUG
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debugger.Break();
                }
                else if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    System.Diagnostics.Debugger.Launch();
                }
#endif
                Log.LogWarning(@$ "Encountered Fatal error while processing image:
{JsonSerializer.Serialize(outputImage)}");
                throw;
            }
        }
 public void LoadShouldThrowIfFileDoesNotExist() =>
 Assert.Throws <FileNotFoundException>(() => ImageBase.Load($"{Path.GetRandomFileName()}.svg"));