Use local fonts to keep the letter shapes and emoji colors in the PNG.
dotnet add package RecursiveAI.OneImage.SvgRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneImage; using OneImage.Svg; SvgFontOptions fonts = new() { UseSystemFonts = false }; fonts.AddFont("RobotoFlex.ttf"); fonts.AddFont("NotoEmoji.ttf"); var config = new ImageConfiguration() .Add(new SvgFormat(fonts: fonts)); var svg = Image.Load("badge.svg", new LoadOptions { Configuration = config }); svg.Save("badge.png"); Console.WriteLine( $"{svg.Width}×{svg.Height} PNG");
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Writes badge.png. Keep badge.svg, RobotoFlex.ttf, and NotoEmoji.ttf from the zip in the project directory when you run it. The SVG names Noto Color Emoji COLR for the dog emoji; COLR is an OpenType format for color glyphs:
<svg xmlns="http://www.w3.org/2000/svg" width="240" height="80"> <rect width="240" height="80" rx="12" fill="#fff"/> <text x="12" y="54" font-family="Roboto Flex" font-size="44" font-weight="750" style="font-variation-settings: 'wdth' 125" >Wide</text> <text x="174" y="54" font-size="44" font-family="Noto Color Emoji COLR" >🐶</text> </svg>
dotnet run prints:
240×80 PNG
RobotoFlex.ttf is a subset of Roboto Flex, a variable font with adjustable weight and width. NotoEmoji.ttf is a subset of Noto Color Emoji COLR, which contains color drawings for the emoji it covers. Both come with their SIL Open Font License 1.1 notices in the zip. font-family uses the font’s internal family name, not its file name.
If there’s no usable font for a text element, it draws nothing even though the rest of the SVG can still render.
Variations
Make the word lighter. In badge.svg, replace the first <text> element with this one. The explicit wght value of 300 overrides font-weight="750". This also drops the custom width setting.
<text x="12" y="54" font-family="Roboto Flex" font-size="44" font-weight="750" style="font-variation-settings: 'wght' 300" >Wide</text>
Use a different emoji. Replace the second <text> element with this one. The included Noto subset has the bee as well as the dog.
<text x="174" y="54" font-size="44" font-family="Noto Color Emoji COLR" >🐝</text>
Related recipes
- Convert SVG to PNG in C#: choose the PNG’s size at render time.
- Draw text on an image in C#: draw a label directly on a photo without SVG.
The short answer
To render SVG with emoji and a variable font to PNG in C#, use RecursiveAI.OneImage.Svg 1.11.4 or later. Add the font files to SvgFontOptions, pass those options to SvgFormat, then load the SVG and save it as PNG. Use font-weight for the weight, font-variation-settings for explicit axis values, and name the color font on the element or span containing the emoji.
How do I convert SVG text to PNG without losing emoji color?
Keep the emoji as a character in the SVG and choose a color font that contains that character. The example puts 🐶 in its own <text> element and names Noto Color Emoji COLR. COLR v1 describes the glyph’s colored shapes, so the renderer draws them at the requested size. OneImage also supports color drawings stored as OpenType SVG glyphs and bitmaps stored in CBDT or sbix tables. With a bitmap glyph, the source is a fixed-size image, not a vector drawing.
The PNG contains pixels rather than editable text or embedded fonts. Someone opening it doesn’t need the font files. The machine doing the conversion still needs them, and the font must cover the exact character or sequence you use. If an emoji comes out monochrome, check that the chosen font has a color glyph for it. If you reuse the bundled subset for a different badge, check its coverage first; it doesn’t contain the full Noto emoji set.
How do I adjust the weight of a variable font in SVG?
Use a font with a weight axis (wght) and set font-weight, as the example does with 750. A variable font stores a range of shapes in one file, so you can choose an intermediate weight without supplying separate regular and bold files. Roboto Flex also has a width axis (wdth), which the badge sets to 125 using the CSS font-variation-settings property.
Use the style attribute or a stylesheet for font-variation-settings. On the same axis it takes precedence over higher-level properties, so 'wght' 300 wins over font-weight="750". OneImage clamps values to the font’s supported range and ignores axes that the font doesn’t have. See the SVG text documentation for how stretch, slant and optical size are chosen.
Why does my font change when I save an SVG as PNG?
An SVG can name a font without carrying it. Your browser might find it on your laptop while the converter on another machine can’t. Register the font from your C# code, check its internal family name against font-family, and check that the font includes the characters you’re drawing. OneImage won’t fetch an external @font-face source named by the SVG.
UseSystemFonts = false makes this example independent of fonts installed on the machine. With system font scanning enabled, OneImage can use installed fonts for characters that aren’t covered by the named or configured fallback families. That’s useful when you want a substitute, but it can hide a missing font and change the appearance when you move to another machine.