Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Microsoft.DotNet.Interactive.AIUtilities/Image.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using Pocket;
using SkiaSharp;

namespace Microsoft.DotNet.Interactive.AIUtilities;

using Logger = Pocket.Logger<SKImage>;

public static class Image {
/// <summary>
/// Generates a base64 encoded string for the image.
/// </summary>
/// <param name="image"></param>
/// <returns>The base64 encoding of the image as png</returns>
public static string ToBase64(this SKImage image)
{
Logger.Log.Event();
var encoded = image.Encode(SKEncodedImageFormat.Png, 100);
return Convert.ToBase64String(encoded.ToArray());
}

/// <summary>
/// Generates a data URI for the image.
/// This can be used in the src attribute of an img tag to display the image in a browser.
/// </summary>
/// <param name="image"></param>
/// <returns>A string in the format data:image/jpeg;base64,{base64EncodedImage}</returns>
public static string ToImageSrc(this SKImage image)
{
Logger.Log.Event();
var base64Image = image.ToBase64();
return $"data:image/png;base64,{base64Image}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.DeepDev.TokenizerLib" Version="1.3.2" />
<PackageReference Include="SkiaSharp" Version="2.88.6" />
<PackageReference Include="System.Reactive" Version="$(SystemReactiveVersion)" />
<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="PocketLogger" Version="0.8.2">
Expand Down