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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 08:48:06  来源:igfitidea点击:

Alpha blending colors in .NET Compact Framework 2.0

提问by Adam Haile

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. I tried this with the following:

显然,第一个甚至不允许您输入 alpha 值,但后者的文档显示“val”是一个 32 位 ARGB 值(作为 0xAARRGGBB,而不是标准的 24 位 0xRRGGBB),所以它是有意义的您可以构建 ARGB 值并将其传递给函数。我用以下方法尝试了这个:

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 上工作?

采纳答案by S?ren Kuklau

Apparently, it's not quite that simple, but still possible, if you have Windows Mobile 5.0 or newer.

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

回答by Adam Haile

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.

哇...如果我必须将所有代码放入(并进行本机互操作!)绝对不值得,但很高兴知道,感谢提供链接。

回答by Peter Walke

There is a codeplex siteout there that seems to do the heavy lifting of com interop for you:

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

回答by fede

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

在初始化的例程中,它工作得很好,谢谢

回答by Ben Kunz

CE 6.0 does not support alpha blending. WM 5.0 and above do, but not through the .NET CF, you will need to P/Invoke GDI stuff to do so. 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 不支持 alpha 混合。WM 5.0 及更高版本可以,但不能通过 .NET CF,您需要 P/Invoke GDI 来执行此操作。那里有现成的解决方案,但是,如果您感兴趣,我明天可以在办公室挖掘链接。我目前必须使用 CE 6.0,所以我不考虑它们。

If you are using CE 6.0 you can use pseudo-transparency by reserving a transparency background color (e.g. ff00ff or something similiarly ugly) and using that in your images for transparent areas. 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.

如果您使用的是 CE 6.0,您可以通过保留透明背景颜色(例如 ff00ff 或类似丑陋的颜色)并在您的图像中将其用于透明区域来使用伪透明。然后,您的父控件必须实现一个简单的接口,允许在子控件的图形缓冲区上重新绘制相关部分。请注意,这不会为您提供真正的“alpha 通道”,而只是一种硬开关二进制透明度。

It's not as bad as it sounds. Take a look at the OpenNETCF ImageButton for starters. 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.

这并不像听起来那么糟糕。看看初学者的 OpenNETCF ImageButton。如果您打算使用这种方法,我有一些简单控件的扩展版本,如果您有兴趣,可以使用这种技术。

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 (i.e. also the stock winforms components) you are out of luck.

另一个缺点是这种技术依赖于父控件实现一个特殊的接口,而子控件在绘图中使用它。因此,对于闭源组件(即还有股票的 winforms 组件),您就不走运了。