-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathKernelExtension.cs
42 lines (38 loc) · 1.48 KB
/
KernelExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Formatting;
using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags;
namespace Dotnet.Interactive.Extension.System.Drawing
{
public class KernelExtension : IKernelExtension
{
public async Task OnLoadAsync(Kernel kernel)
{
Formatter.Register<Image>((image, writer) =>
{
var id = Guid.NewGuid().ToString("N");
using var stream = new MemoryStream();
image.Save(stream, ImageFormat.Png);
stream.Flush();
var data = stream.ToArray();
var imgTag = CreateImgTag(data, id, image.Height, image.Width);
writer.Write(imgTag);
}, HtmlFormatter.MimeType);
await kernel.SendAsync(
new DisplayValue(new FormattedValue(
"text/markdown",
$"Added support for System.Drawing to kernel {kernel.Name}.")));
}
private static PocketView CreateImgTag(byte[] data, string id, int height, int width)
{
var imageSource = $"data:image/png;base64, {Convert.ToBase64String(data)}";
PocketView imgTag = img[id: id, src: imageSource, height: height, width: width]();
return imgTag;
}
}
}