向图标添加叠加层时的透明度问题
我有一个 .ico
文件,其中包含一个 .png
文件,其中包含我想要应用于图标的覆盖层。我在这方面非常缺乏经验,所以设法从互联网上获取一些代码,直到我有了一些几乎可以工作的东西。
问题是透明度丢失并被白色取代。
另外,我认为颜色范围减少了。我添加了一些调试代码(已注释掉)以分两个阶段保存图标。当我在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;
}
I've got an .ico
file with a .png
file that has an overlay I want to apply to the icon. I'm very inexperienced in this so managed to get bits of code off the internet until I had something that almost works.
The problem is that the transparency is lost and replaced with white.
Also, I think the range of colours is reduced. I added some debug code (commented out) to save the icon at 2 stages. When I edit it at the first stage in VS 2010, the colour pallete has 16 colours, stage1.ico has many more.
Seems to be the Icon.FromHandle
causing the problems. The function below takes two ImageSource parameters. The first comes from the .ico
file and the second from the .png
file (overlay).
What should I be doing instead?
Function -
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够使用下面的代码动态创建一个具有透明度的图标,用作覆盖图标。对于我的程序,我想要一个显示排队的新消息数量的数字。原谅VB...
I was able to create an icon dynamically with transparency for use as an overlay icon with the code below. For my program I wanted a number displaying how many new messages were queued up. Pardon the VB...