简体   繁体   English

.NET Compact Framework 2.0 中的 Alpha 混合颜色

[英]Alpha blending colors in .NET Compact Framework 2.0

In the Full .NET framework you can use the Color.FromArgb() method to create a new color with alpha blending, like this:在完整的 .NET 框架中,您可以使用 Color.FromArgb() 方法创建具有 alpha 混合的新颜色,如下所示:

Color blended = Color.FromArgb(alpha, color);

or或者

Color blended = Color.FromArgb(alpha, red, green , blue);

However in the Compact Framework (2.0 specifically), neither of those methods are available, you only get:但是在 Compact Framework(特别是 2.0)中,这两种方法都不可用,您只能获得:

Color.FromArgb(int red, int green, int blue);

and

Color.FromArgb(int val);

The first one, obviously, doesn't even let you enter an alpha value, but the documentation for the latter shows that "val" is a 32bit ARGB value (as 0xAARRGGBB as opposed to the standard 24bit 0xRRGGBB), so it would make sense that you could just build the ARGB value and pass it to the function.显然,第一个甚至不允许您输入 alpha 值,但后者的文档显示“val”是一个 32 位 ARGB 值(作为 0xAARRGGBB,而不是标准的 24 位 0xRRGGBB),所以它是有意义的您可以构建 ARGB 值并将其传递给函数。 I tried this with the following:我用以下方法尝试了这个:

private Color FromARGB(byte alpha, byte red, byte green, byte blue)
{
    int val = (alpha << 24) | (red << 16) | (green << 8) | blue;
    return Color.FromArgb(val);
}

But no matter what I do, the alpha blending never works, the resulting color always as full opacity, even when setting the alpha value to 0.但是无论我做什么,alpha 混合都不起作用,即使将 alpha 值设置为 0,生成的颜色也总是完全不透明。

Has anyone gotten this to work on the Compact Framework?有没有人让它在 Compact Framework 上工作?

显然,这不是那么简单,但如果您有 Windows Mobile 5.0 或更新版本, 仍然可以

有一个codeplex 站点似乎为您完成了 com interop 的繁重工作:

CE 6.0 does not support alpha blending. CE 6.0 不支持 alpha 混合。 WM 5.0 and above do, but not through the .NET CF, you will need to P/Invoke GDI stuff to do so. WM 5.0 及更高版本可以,但不能通过 .NET CF,您需要 P/Invoke GDI 来执行此操作。 There are ready-made solutions out there, however, if you are interested i can dig the links out tomorrow at the office.那里有现成的解决方案,但是,如果您感兴趣,我明天可以在办公室挖掘链接。 I have to work with CE 6.0 currently so i don't have them on my mind.我目前必须使用 CE 6.0,所以我不考虑它们。

If you are using CE 6.0 you can use pseudo-transparency by reserving a transparency background color (eg ff00ff or something similiarly ugly) and using that in your images for transparent areas.如果您使用的是 CE 6.0,您可以通过保留透明背景颜色(例如 ff00ff 或类似丑陋的颜色)并在您的图像中将其用于透明区域来使用伪透明。 Then, your parent controls must implement a simple interface that allows re-drawing the relevant portion on your daughter controls' graphics buffer.然后,您的父控件必须实现一个简单的接口,允许在子控件的图形缓冲区上重新绘制相关部分。 Note that this will not give you a true "alpha channel" but rather just a hard on-off binary kind of transparency.请注意,这不会为您提供真正的“alpha 通道”,而只是一种硬开关二进制透明度。

It's not as bad as it sounds.这并不像听起来那么糟糕。 Take a look at the OpenNETCF ImageButton for starters.看看初学者的 OpenNETCF ImageButton。 If you are going to use this method, i have a somewhat extended version of some simple controls with this technique lying around if you are interested.如果您打算使用这种方法,我有一些简单控件的扩展版本,如果您有兴趣,可以使用这种技术。

One additional drawback is that this technique relies on the parent control implementing a special interface, and the daugther controls using it in drawing.另一个缺点是这种技术依赖于父控件实现一个特殊的接口,而子控件在绘图中使用它。 So with closed-source components (ie also the stock winforms components) you are out of luck.因此,对于闭源组件(即还有股票的 winforms 组件),您就不走运了。

Apparently, it's not quite that simple, but still possible, if you have Windows Mobile 5.0 or newer.显然,这不是那么简单,但如果您有 Windows Mobile 5.0 或更新版本,它仍然是可能的。

Wow...definitely not worth it if I have to put all that code in (and do native interop!) Good to know though, thanks for the link.哇...如果我必须将所有代码放入(并进行本机互操作!)绝对不值得,但很高兴知道,感谢提供链接。

i take this code and i add this我拿了这个代码,我添加了这个

device.RenderState.AlphaBlendEnable = true;
device.RenderState.AlphaFunction = Compare.Greater;
device.RenderState.AlphaTestEnable = true;
device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
device.RenderState.SourceBlend = Blend.SourceAlpha;
device.RenderState.DiffuseMaterialSource = ColorSource.Material;

in the initialized routine and it work very well, thank you在初始化的例程中,它工作得很好,谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM