spectre.console/docs/input/widgets/canvas-image.md
Patrik Svensson 8261b25e5c Fix tree rendering
Fixes some tree rendering problems where lines were not properly drawn
at some levels during some circumstances.

* Change the API back to only allow one root.
* Now uses a stack based approach to rendering instead of recursion.
* Removes the need for measuring the whole tree in advance.
  Leave this up to each child to render.
2021-01-10 15:55:11 +01:00

36 KiB

Title: Canvas Image Order: 70

To add ImageSharp superpowers to your console application to draw images, you will need to install the Spectre.Console.ImageSharp NuGet package.

> dotnet add package Spectre.Console.ImageSharp

Loading images

Once you've added the Spectre.Console.ImageSharp NuGet package, you can create a new instance of CanvasImage to draw images to the console.

// Load an image
var image = new CanvasImage("cake.png");

// Set the max width of the image.
// If no max width is set, the image will take
// up as much space as there is available.
image.MaxWidth(16);

// Render the image to the console
AnsiConsole.Render(image);

Result

                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                
                                

Manipulating images

You can take full advantage of ImageSharp and manipulate images directly via it's Processing API.

// Load an image
var image = new CanvasImage("cake.png");
image.MaxWidth(32);

// Set a sampler that will be used when scaling the image.
image.BilinearResampler();

// Mutate the image using ImageSharp
image.Mutate(ctx => ctx.Grayscale().Rotate(-45).EntropyCrop());

// Render the image to the console
AnsiConsole.Render(image);

Result