Implemented buffer/stream constructors for CanvasImage (#246)

* Implemented buffer/stream constructors for CanvasImage and added section to Canvas example

Signed-off-by: David Butler <mail@davidbutlerdesign.co.uk>
This commit is contained in:
David Butler 2021-01-27 17:12:22 +00:00 committed by GitHub
parent ad49b6aa67
commit 953008b5e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 2 deletions

View File

@ -14,9 +14,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="cake.png"> <EmbeddedResource Include="cake.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </EmbeddedResource>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,3 +1,5 @@
using System.Diagnostics;
using System.Reflection;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using Spectre.Console; using Spectre.Console;
using Spectre.Console.Rendering; using Spectre.Console.Rendering;
@ -23,6 +25,16 @@ namespace CanvasExample
image.NoMaxWidth(); image.NoMaxWidth();
image.Mutate(ctx => ctx.Grayscale().Rotate(-45).EntropyCrop()); image.Mutate(ctx => ctx.Grayscale().Rotate(-45).EntropyCrop());
Render(image, "Image from file (fit, greyscale, rotated)"); 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) private static void Render(IRenderable canvas, string title)

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Transforms; using SixLabors.ImageSharp.Processing.Processors.Transforms;
@ -51,6 +52,24 @@ namespace Spectre.Console
Image = SixLabors.ImageSharp.Image.Load<Rgba32>(filename); Image = SixLabors.ImageSharp.Image.Load<Rgba32>(filename);
} }
/// <summary>
/// Initializes a new instance of the <see cref="CanvasImage"/> class.
/// </summary>
/// <param name="data">Buffer containing an image.</param>
public CanvasImage(ReadOnlySpan<byte> data)
{
Image = SixLabors.ImageSharp.Image.Load<Rgba32>(data);
}
/// <summary>
/// Initializes a new instance of the <see cref="CanvasImage"/> class.
/// </summary>
/// <param name="data">Stream containing an image.</param>
public CanvasImage(Stream data)
{
Image = SixLabors.ImageSharp.Image.Load<Rgba32>(data);
}
/// <inheritdoc/> /// <inheritdoc/>
protected override Measurement Measure(RenderContext context, int maxWidth) protected override Measurement Measure(RenderContext context, int maxWidth)
{ {