mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-13 15:42:50 +08:00
44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using SixLabors.ImageSharp.Processing;
|
|
using Spectre.Console;
|
|
using Spectre.Console.Rendering;
|
|
|
|
namespace Canvas;
|
|
|
|
public static class Program
|
|
{
|
|
public static void Main()
|
|
{
|
|
// Draw an image using CanvasImage powered by ImageSharp.
|
|
// This requires the "Spectre.Console.ImageSharp" NuGet package.
|
|
var image = new CanvasImage("cake.png");
|
|
image.BilinearResampler();
|
|
image.MaxWidth(16);
|
|
Render(image, "Image from file (16 wide)");
|
|
|
|
// Draw image again, but without max width
|
|
image.NoMaxWidth();
|
|
image.Mutate(ctx => ctx.Grayscale().Rotate(-45).EntropyCrop());
|
|
Render(image, "Image from file (fit, greyscale, rotated)");
|
|
|
|
// Draw image again, but load from embedded resource rather than file
|
|
using (var fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Canvas.cake.png"))
|
|
{
|
|
Debug.Assert(fileStream != null);
|
|
var embeddedImage = new CanvasImage(fileStream);
|
|
embeddedImage.BilinearResampler();
|
|
embeddedImage.MaxWidth(16);
|
|
Render(embeddedImage, "Image from embedded resource (16 wide)");
|
|
}
|
|
}
|
|
|
|
private static void Render(IRenderable canvas, string title)
|
|
{
|
|
AnsiConsole.WriteLine();
|
|
AnsiConsole.Write(new Rule($"[yellow]{title}[/]").LeftJustified().RuleStyle("grey"));
|
|
AnsiConsole.WriteLine();
|
|
AnsiConsole.Write(canvas);
|
|
}
|
|
}
|