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

发布于 2024-10-19 05:02:29 字数 1505 浏览 7 评论 0原文

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

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


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


我将使用 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;

I'm developing a program that makes use of Windows 7 (and Vista) taskbar features. Right now I have a custom bitmap that will be shown in the taskbar thumbnail. The bitmap is created programmatic and shows up successfully. The only 'problem' I'm having is that want to use transparent in that image, which should show transparent in the thumbnail too. But without any success, resulting in the standard Light-Gray color.

I've seen evidence that programs successfully get transparent in the their images:

Now is my question: how do I get transparent in my thumbnail image?

I'll be filling the image with the Graphics class, so anything is allowed.
I should mention that I use Windows® API Code Pack, which uses GetHbitmap to
set the image as thumbnail.

EDIT:
Just to make it complete, this is the code I'm using 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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

焚却相思 2024-10-26 05:02:29

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

以下是如何创建带有 Alpha 通道的位图并使其默认透明:

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

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

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

graphics.CompositingMode = CompositingMode.SourceCopy;

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

What pixel format is your Bitmap? If it has no alpha channel, you won't be able to store transparency information in your image.

Here is how to create a bitmap with an alpha channel and make it transparent by default:

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

You can then draw anything you want, including semi transparent stuff using the alpha channel.

Also note that if you are trying to paint transparency over existing opaque stuff (say to make a hole), you need to change compositing mode:

graphics.CompositingMode = CompositingMode.SourceCopy;

This will make whatever color you use to overwrite the one in the image rather than blending with it.

☆獨立☆ 2024-10-26 05:02:29

System.Drawing.Bitmap 支持 Alpha 级别。所以最简单的方法是

Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(Brushes.Transparent, 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));

你也可以通过替换 Brushes.Transparent 来获得部分透明度

new SolidBrush(Color.FromArgb(150, 255, 255, 255));

System.Drawing.Bitmap supports an alpha level. So the simplest way is

Graphics g = Graphics.FromImage(bmp);
g.FillRectangle(Brushes.Transparent, 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));

however you can also have partial transparency by replacing Brushes.Transparent with

new SolidBrush(Color.FromArgb(150, 255, 255, 255));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文