对我来说,这个解决方案工作。
与代码Write text on bitmap in C#
private Icon DrawIcon(Brush brush)
{
//https://stackoverflow.com/questions/6311545/c-sharp-write-text-on-bitmap
Bitmap bmp = new Bitmap(MyNameSpace.Properties.Resources.desktop_windows_48px);
// Create a rectangle for the entire bitmap
RectangleF rectf = new RectangleF(0, 0, bmp.Width, bmp.Height);
// Create graphic object that will draw onto the bitmap
Graphics g = Graphics.FromImage(bmp);
// The smoothing mode specifies whether lines, curves, and the edges of filled areas use smoothing (also called antialiasing). One exception is that path gradient brushes do not obey the smoothing mode. Areas filled using a PathGradientBrush are rendered the same way (aliased) regardless of the SmoothingMode property.
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// The interpolation mode determines how intermediate values between two endpoints are calculated.
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// Use this property to specify either higher quality, slower rendering, or lower quality, faster rendering of the contents of this Graphics object.
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
// This one is important
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
// Draw the text onto the image
g.FillRectangle(brush, new Rectangle(2, 4, 20, 12));
// Flush all graphics changes to the bitmap
g.Flush();
Bitmap temp = bmp;
// Get an Hicon for myBitmap.
IntPtr Hicon = temp.GetHicon();
// Create a new icon from the handle.
Icon newIcon = Icon.FromHandle(Hicon);
return newIcon;
}
我也使用NotifyIcon
但不会为你改变很多创建此。
_notifyIcon.Icon = DrawIcon(Brushes.Blue);
MainWindow.Icon = Imaging.CreateBitmapSourceFromHIcon(_notifyIcon.Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
Lateron我改变这样的时候才改变图标源(可能是类似
private void App_StateChanged(object sender, StateChangedEventArgs e)
{
if(e.State == ApplicationState.Defective)
{
//Show window on error !
ShowMainWindow();
//(sender as Window).Activate();
}
_notifyIcon.Icon = DrawIcon(e.StateBrush);
MainWindow.Icon = Imaging.CreateBitmapSourceFromHIcon(_notifyIcon.Icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
_notifyIcon.Visible = true;
}
它看起来与此类似:
默认:
出错:
希望这对您有所帮助!
您是否尝试过加入你的2个图标到你'你的项目Properties.Resources',然后设置'MainWindow.Icon'到? - 对我来说,这是工作.. –
@FelixD。 “Properties.Resources”是什么意思?我在我的项目属性面板中的应用程序(选项卡) - >资源(部分) - >图标也许这阻止更改? – Safe
在你的项目中展开'Properties'并双击'Resources.resx',然后你可以点击'Add Resource - > Add Existing File'并添加你的图标。这使得它们可以通过'YourProjectName.Properties.Resources.NameOfIcon'在代码中使用。它们会自动添加到项目中的资源文件夹中。确保将“BuildOption”更改为“Resource”。 –