c# - 如何使任务栏缩略图上的区域在 Windows 7 中显示为透明?

标签 c# .net windows-7 taskbar dwm

我正在开发一个利用 Windows 7(和 Vista)任务栏功能的程序。现在我有一个自定义位图,它将显示在任务栏缩略图 中。位图以编程方式创建并成功显示。我遇到的唯一“问题”是想在该图像中使用透明,缩略图也应该显示透明。但没有任何成功,导致标准的Light-Gray颜色。

我已经看到程序成功地在图像中变得透明的证据:


现在是我的问题:如何使缩略图透明?


我将用 Graphics 类填充图像,所以任何东西都是允许的。
我应该提到我使用 Windows® API Code Pack ,它使用 GetHbitmap 来 将图像设置为缩略图。

编辑:
为了使其完整,这是我使用 atm 的代码:

Bitmap bmp = new Bitmap(197, 119);

Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0, 0, bmp.Width, bmp.Height));  // Transparent is actually light-gray;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
g.DrawString("Information:", fontHeader, brush, new PointF(5, 5));

bmp.MakeTransparent(Color.Red);
return bmp;

最佳答案

您的位图是什么像素格式?如果它没有 alpha channel ,您将无法在图像中存储透明度信息。

以下是如何创建带 alpha channel 的位图并使其默认透明:

Bitmap image = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using(Graphics graphics = Graphics.FromImage(image))
{
    graphics.Clear(Color.Transparent);
    // Draw your stuff
}

然后您可以绘制任何您想要的东西,包括使用 alpha channel 的半透明内容。

另请注意,如果您尝试在现有的不透明物体上绘制透明度(比如打洞),则需要更改合成模式:

graphics.CompositingMode = CompositingMode.SourceCopy;

这将使您使用的任何颜色覆盖图像中的颜色,而不是与之混合。

关于c# - 如何使任务栏缩略图上的区域在 Windows 7 中显示为透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132760/

相关文章:

c# - 在不同的CPU内核上执行上下文切换和线程

powershell - 如何在Powershell中为 “Network Service”帐户创建Windows服务?

windows-7 - 如何在 UAC 对话框中控制我的应用程序名称?

c# - 如何以字母数字升序对这个字符串进行排序

c# - 在c#中分层显示父子元素

c# - 检查对象是否是委托(delegate)

c# - 奇怪的 LINQ 到实体异常

c# - 更改数据表中的行位置或移动数据表中的行

c# - 将参数传递给 CaSTLe Windsor Typed Factory

css - Windows 7 上的 Chrome 中出现白框