<PackageReference Include="System.Drawing.Common" Version="10.0.0-preview.2.25163.9" />

PrivateFontCollection

public sealed class PrivateFontCollection : FontCollection
Provides a collection of font families built from font files that are provided by the client application.
using System.IO; using System.Runtime.CompilerServices; using Windows.Win32; using Windows.Win32.Graphics.Gdi; using Windows.Win32.Graphics.GdiPlus; namespace System.Drawing.Text { public sealed class PrivateFontCollection : FontCollection { public unsafe PrivateFontCollection() : base(Create()) { } private unsafe static GpFontCollection* Create() { GpFontCollection* result = default(GpFontCollection*); PInvokeGdiPlus.GdipNewPrivateFontCollection(&result).ThrowIfFailed(); return result; } protected unsafe override void Dispose(bool disposing) { GpFontCollection* ptr = this.Pointer(); if (ptr != null) PInvokeGdiPlus.GdipDeletePrivateFontCollection(&ptr); base.Dispose(disposing); } [NullableContext(1)] public unsafe void AddFontFile(string filename) { ArgumentNullException.ThrowIfNull(filename, "filename"); if (!File.Exists(filename)) throw new FileNotFoundException(); IntPtr intPtr; if (filename == null) intPtr = (IntPtr)(void*)null; else { ref reference = ref filename.GetPinnableReference(); intPtr = (IntPtr)(&reference); } char* value = (char*)(long)intPtr; PInvokeGdiPlus.GdipPrivateAddFontFile(this.Pointer(), value).ThrowIfFailed(); GC.KeepAlive(this); ref reference = ref *(char*)null; GdiAddFontFile(filename); } public unsafe void AddMemoryFont(IntPtr memory, int length) { PInvokeGdiPlus.GdipPrivateAddMemoryFont(this.Pointer(), (void*)(long)memory, length).ThrowIfFailed(); GC.KeepAlive(this); } [NullableContext(1)] private unsafe static void GdiAddFontFile(string filename) { IntPtr value; if (filename == null) value = (IntPtr)(void*)null; else { ref reference = ref filename.GetPinnableReference(); value = (IntPtr)(&reference); } PInvoke.AddFontResourceEx((char*)(long)value, FONT_RESOURCE_CHARACTERISTICS.FR_PRIVATE, null); ref reference = ref *(char*)null; } } }