AvatarGenerator Class

Generates avatars from names and options, as SVG strings or as scenes of Meziantou.Framework.VectorGraphics.

public static class AvatarGenerator

Methods

NameDescription
CreateScene(string)Creates the avatar of the specified name as a scene, using the default options.
CreateScene(string, AvatarOptions)Creates the avatar of the specified name as a scene.
CreateScene(string, AvatarOptions, ITextMeasurer?)Creates the avatar of the specified name as a scene, laid out with the specified fonts.
CreateSvg(string)Creates an avatar SVG string for the specified name, using the default options.
CreateSvg(string, AvatarOptions)Creates an avatar SVG string for the specified name and options.

CreateScene overloads

CreateScene(string)

AvatarGenerator.cs

Creates the avatar of the specified name as a scene, using the default options.

public static Scene CreateScene(string name)
Parameters
name string
The full name used to compute the color and the bigram.
Returns

Scene The scene of the avatar.

Remarks

The scene can be written as SVG with SvgSceneRenderer, or drawn as an image (PNG, WebP, JPEG...) with Meziantou.Framework.Imaging.VectorGraphics.

CreateScene(string, AvatarOptions)

AvatarGenerator.cs

Creates the avatar of the specified name as a scene.

public static Scene CreateScene(string name, AvatarOptions options)
Parameters
name string
The full name used to compute the color and default bigram.
options AvatarOptions
The generation options.
Returns

Scene The scene of the avatar.

Remarks

The scene can be written as SVG with SvgSceneRenderer, or drawn as an image (PNG, WebP, JPEG...) with Meziantou.Framework.Imaging.VectorGraphics.

CreateScene(string, AvatarOptions, ITextMeasurer?)

AvatarGenerator.cs

Creates the avatar of the specified name as a scene, laid out with the specified fonts.

public static Scene CreateScene(string name, AvatarOptions options, ITextMeasurer? textMeasurer)
Parameters
name string
The full name used to compute the color and default bigram.
options AvatarOptions
The generation options.
textMeasurer ITextMeasurer?
The measurer of the fonts that draw the scene, or null for AfmTextMeasurer.Default. The initials are centered and sized with its metrics, so pass the measurer of the fonts that draw the image.
Returns

Scene The scene of the avatar.

Remarks

The avatar is the same as the one of AvatarGenerator.CreateSvg: same shape, colors and bigram. The initials are drawn in the bold font of the measurer, centered vertically, and with a smaller font when they would be wider than the shape. The fonts of the measurer (ITextMeasurer.Fonts) become the fonts of the scene, so the renderers draw the initials with the fonts that measured them, including the fallback fonts of the characters the main font does not have. The title of the scene is the accessible label, or nothing when AvatarOptions.IsDecorative is true.

The colors of every pair of the palette must be colors that SceneColor.Parse accepts: #rrggbb, rgb(), hsl(), a CSS color name... CSS variables and currentColor are only supported by AvatarGenerator.CreateSvg.

Example
// Lay the avatar out with the fonts that draw the image, so the initials are centered
var measurer = SceneImageRenderer.CreateMeasurer();
var scene = AvatarGenerator.CreateScene("John Doe", new AvatarOptions { Size = 128 }, measurer);

using var image = scene.ToImage();
Assert.Equal((128, 128), (image.Width, image.Height));

// Save to a file (image.Save("avatar.png")) or to a stream, in any format
using var png = new MemoryStream();
image.Save(png, new PngEncoder());

CreateSvg overloads

CreateSvg(string)

AvatarGenerator.cs

Creates an avatar SVG string for the specified name, using the default options.

public static string CreateSvg(string name)
Parameters
name string
The full name used to compute the color and the bigram.
Returns

string The generated SVG string.

CreateSvg(string, AvatarOptions)

AvatarGenerator.cs

Creates an avatar SVG string for the specified name and options.

public static string CreateSvg(string name, AvatarOptions options)
Parameters
name string
The full name used to compute the color and default bigram.
options AvatarOptions
The generation options.
Returns

string The generated SVG string.

Remarks

Characters that are not valid in an XML document are removed from name. An explicit AvatarOptions.Bigram containing such characters is rejected instead.