Search code examples
c#winformsfontsnotifyicon

Use the Segoe MDL2 Assets font as an icon for NotifyIcon


Is it possible to use the Segoe MDL2 Assets font as an icon for a SystemTray app (NotifyIcon)?

I tried using an answer from this question but it did not work:

but it did not work


Solution

  • You need to pass the right Unicode Point of the required glyph.

    Please consider the following snippet:

    using System.Drawing;
    using System.Drawing.Text;
    
    //...
    
    public enum Glyphs
    {
        GlobalNavigationButton = 0xE700,
        Wifi = 0xE701,
        Bluetooth = 0xE702,
        Connect = 0xE703,
        InternetSharing = 0xE704,
        VPN = 0xE705,
        //Add more...
    }
    
    //...
    
    public static Icon CreateGlyphIcon(Glyphs glyph)
    {
        using (var b = new Bitmap(16, 16))
        using (Graphics g = Graphics.FromImage(b))
        using (var f = new Font("Segoe MDL2 Assets", 12, FontStyle.Regular))
        using (var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
        {
            g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
            g.DrawString(((char)glyph).ToString(), f, Brushes.White, new Rectangle(0, 0, 16, 16), sf);
    
            return Icon.FromHandle(b.GetHicon());
        }                   
    }
    

    Usage:

    var icon = CreateGlyphIcon(Glyphs.Connect);
    
    //or
    
    var icon = CreateGlyphIcon(Glyphs.Bluetooth);
    
    //...etc.