Category / Section
How to set custom font to WinForms Grid without installing it in machine?
2 mins read
To draw the WinForms Grid Control with custom font or private font without installing in the machine, use the AddMemoryFont() method of Syncfusion.Drawing.FontUtil class.
Code Snippet
C#
public Form1() { axelFont = InitializeResourceFont(Properties.Resources.Axel_Regular); //Setting the font to grid. gridControl1.Font = new Font(axelFont.FontFamily, 15); } private Font InitializeResourceFont(byte[] resourceFont) { byte[] fontData = resourceFont; fonts.AddFontFile(@"..\..\Resources\Axel-Regular.ttf"); IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length); Marshal.Copy(fontData, 0, fontPtr, fontData.Length); //To draw the font using Syncfusion drawing to use the private font collections FontUtil.AddMemoryFont(fontPtr, resourceFont.Length); Marshal.FreeCoTaskMem(fontPtr); return new Font(fonts.Families[0], 9.00F); }
VB
Public Sub New() axelFont = InitializeResourceFont(My.Resources.Axel_Regular 'Setting the font to grid. gridControl1.Font = New Font(axelFont.FontFamily, 15) End Sub Private Function InitializeResourceFont(ByVal resourceFont() As Byte) As Font Dim fontData() As Byte = resourceFont fonts.AddFontFile("..\..\Resources\Axel-Regular.ttf") Dim fontPtr As IntPtr = Marshal.AllocCoTaskMem(fontData.Length) Marshal.Copy(fontData, 0, fontPtr, fontData.Length) 'To draw the font using Syncfusion drawing to use the private font collections FontUtil.AddMemoryFont(fontPtr, resourceFont.Length) Marshal.FreeCoTaskMem(fontPtr) Return New Font(fonts.Families(0), 9.00F) End Function
Screenshot
Sample Links:
C#: Adding custom font to grid_CS
VB: Adding custom font to grid_VB