Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text to icon in c#?

Tags:

c#

.net

wpf

gdi+

gdi

I want to display an icon [a .ico file] in System tray with some text added to it at runtime. Is there any native WPF way to do it? or snippet for GDI+ also would be grateful.

Thank you.

like image 800
iraSenthil Avatar asked May 11 '11 00:05

iraSenthil


1 Answers

Here is the code that worked for me,

public static Icon GetIcon(string text)
{
    //Create bitmap, kind of canvas
    Bitmap bitmap = new Bitmap(32, 32);

    Icon icon = new Icon(@"Images\PomoDomo.ico");
    System.Drawing.Font drawFont = new System.Drawing.Font("Calibri", 16, FontStyle.Bold);
    System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.White);

    System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);

    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
    graphics.DrawIcon(icon, 0, 0);            
    graphics.DrawString(text, drawFont, drawBrush, 1, 2);

    //To Save icon to disk
    bitmap.Save("icon.ico", System.Drawing.Imaging.ImageFormat.Icon);

    Icon createdIcon = Icon.FromHandle(bitmap.GetHicon());

    drawFont.Dispose();
    drawBrush.Dispose();
    graphics.Dispose();
    bitmap.Dispose();

    return createdIcon;
}
like image 142
iraSenthil Avatar answered Nov 15 '22 04:11

iraSenthil