如何添加文本在C#中的图标?图标、文本

2023-09-03 16:49:52 作者:将妓就计

我要显示在系统托盘中的图标[一个.ico文件]与一些文本添加到它在运行时。 是否有任何本地WPF办法做到这一点?或片段的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.

感谢您。

推荐答案

下面是为我工作的code,

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;
}