對我來說,這個解決方案工作。
與代碼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」。 –