我有一个.ico
文件,其中.png
文件有一个我想要应用于图标的叠加层。我对此非常缺乏经验,因此设法从互联网上获取一些代码,直到我得到了几乎可以工作的东西。
问题在于透明度丢失并被白色取代。
另外,我认为颜色范围减少了。我添加了一些调试代码(注释掉)以在2个阶段保存图标。当我在VS 2010的第一阶段编辑它时,颜色托盘有16种颜色,stage1.ico还有更多颜色。
似乎是造成问题的Icon.FromHandle
。下面的函数有两个ImageSource参数。第一个来自.ico
文件,第二个来自.png
文件(叠加)。
我应该做什么呢?
功能 -
private static Icon Render(ImageSource baseImage, ImageSource overlay)
{
int iconSize = 32;
RenderTargetBitmap renderBitmap
= new RenderTargetBitmap(iconSize,
iconSize,
96, 96,
PixelFormats.Pbgra32);
DrawingVisual visual = new DrawingVisual();
using (DrawingContext context = visual.RenderOpen())
{
context.DrawImage(baseImage, new System.Windows.Rect(0, 0, iconSize, iconSize));
context.DrawImage(overlay, new System.Windows.Rect(0, 0, iconSize, iconSize));
context.Close();
renderBitmap.Render(visual);
}
BmpBitmapEncoder encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
MemoryStream stream = new MemoryStream();
encoder.Save(stream);
Bitmap bmp = new Bitmap(stream);
//bmp.Save("c:\\tmp\\stage1.ico"); // save what we have here
IntPtr Hicon = bmp.GetHicon();
Icon icon = Icon.FromHandle(Hicon);
// Looking at stage2.ico gives a different version to stage1.ico
//using (var fs = new FileStream("c:\\tmp\\stage2.ico", FileMode.Create, FileAccess.Write, FileShare.Delete))
//{
//icon.Save(fs);
//}
return icon;
}
答案 0 :(得分:0)
我能够动态创建一个透明图标,用作下面代码的叠加图标。对于我的程序,我想要一个数字显示有多少新消息排队。请原谅VB ......
Private _counter As Integer = 0
Public Sub NewMessageIncrementOverlay()
_counter += 1
Dim displayVal = If(_counter > 9, "+", _counter.ToString)
Dim bitm As Bitmap = New Bitmap(40, 40,
System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(bitm)
g.FillRectangle(System.Drawing.Brushes.Transparent, 0, 0, 40, 40)
g.FillPie(System.Drawing.Brushes.Red, 0, 0, 40, 40, 0, 360)
g.DrawString(displayVal, New Font("Consolas", 30, FontStyle.Bold),
System.Drawing.Brushes.White, New PointF(3, -5))
If TaskbarManager.IsPlatformSupported Then
Dim icon As Icon = icon.FromHandle(bitm.GetHicon)
TaskbarManager.Instance.SetOverlayIcon(icon, displayVal)
End If
End Sub