每像素 Alpha 混合 [英] Per Pixel Alpha Blend

查看:37
本文介绍了每像素 Alpha 混合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直在尝试实现某种形式,使 PNG 图像具有透明度.

For quite sometime now I have been trying to accomplish some sort of form which allows transparency with PNG images.

我在 C# 中工作,我找到了一个不错的项目,该项目通过每像素 alpha 混合和将图像更改为位图并使用 alpha 层仅显示不透明的部分来实现这一点,这是然后使用 UpdateLayeredWindow 进行更新.

I am working in C# and I was able to find a nice project that achieved this via per pixel alpha blend and by changing the image to a bitmap and using an alpha layer to only show parts which aren't transparent, which is then updated with UpdateLayeredWindow.

这里是项目:http://www.codeproject.com/Articles/1822/Per-Pixel-Alpha-Blend-in-C

我能够实现上述项目中的代码,但由于设置了创建参数,表单基本上不可见,只显示图像.当我删除创建参数时,不会应用效果.

I was able to implement the code from the aforementioned project but due to the create parameters which is set the form is basically made invisible and only the image is shown. And when I remove the create parameters the effect is not applied.

但我希望能够将其应用于我的表单背景.对于我的表单,我将表单边框样式设置为无并应用了背景图像.

But I am wanting to be able to apply this to my form background. For my form I have set the form border style to none and applied a background image.

如何将这种技术应用于我的表单背景,并且仍然能够添加不受影响的控件?

推荐答案

您正在使用分层窗口技术来获得具有 alpha 透明度的背景.这带来了无法看到控件的缺点.那是因为分层窗口只是一个图像.如果你把控件放在上面,你仍然会得到它们的事件和消息,但要真正看到它们,你必须在这个位图上绘制所有的控件并使其与控件状态保持同步.乏味.

You're using the technique of layered windows to be able to have a background with alpha-transparency. This comes with the disadvantage of not being able to see controls on it. That's because the layered window is just an image. If you put controls on it, you will still get their events and messages, but to actually see them, you would have to draw all the controls on this bitmap and keep it up to date with the controls state. Tedious.

虽然没有其他方法可以获得 alpha 形式(至少我知道不是一个好的方法).

There's no other way to get an alpha-form though (at least not a good one I'm aware of).

如果您只关心表单的 alpha 透明边框,并且表单的客户端/内部部分是实心的,您应该考虑创建第二个内容"表单,在构成边框的分层窗口上方,并与分层窗口一起移动它.因此,您可以结合两全其美.

If you're just caring about an alpha-transparent border of the form, and the client / inner part of the form being solid, you should think about creating a second "content" form, above the layered window which makes up the border, and move it with the layered window. Thus, you can combine the best of both worlds.

示例如下:http://www.codeproject.com/Articles/20758/Alpha-Blended-Windows-Forms.它还讨论了在实现这种 2-form-fashion 时的典型陷阱以及如何避免它们.

An example is seen here: http://www.codeproject.com/Articles/20758/Alpha-Blended-Windows-Forms. It also discusses typical traps when implementing such 2-form-fashion and how to avoid them.

Afaik、Office 2013 和 VS 2013 以类似的方式执行此操作(尽管使用类似 WPF 的动力绘图),请注意它们的窗口阴影具有的所有不错的错误.

Afaik, Office 2013 and VS 2013 do it in a similar fashion (with WPF-like powered drawing though), notice all the nice bugs their window shadow has.

另一种选择,如果可以的话:使用 WPF.由于控件绘图完全由您的图形卡呈现,并将其呈现到单个图形缓冲区,因此它可以将此类缓冲区转换为分层窗口的位图,从而使其与控件完美保持同步.我从来没有这样做过,因为我不是 WPF 程序员,但我已经看到了这一点,并且我敢打赌有一些不错的评论者提供了很好的教程链接.

Another choice, if you can choose it: Use WPF. Since the control drawing is completely rendered by your graphics card which renders it to a single graphics buffer, it can turn such buffer into the bitmap of a layered window and thus keep it up to date with the controls perfectly. I've never done that though since I'm not a WPF programmer, but I've seen this in action and bet there are some nice commenters giving links to good tutorials.

这篇关于每像素 Alpha 混合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆