在 C# 中为 Silverlight 的 WriteableBitmap 将 Int 转换为 Color
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1133862/
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
Converting Int to Color in C# for Silverlight's WriteableBitmap
提问by
In Silverlight 3 there is now a WriteableBitmap which provides get/put pixel abilities. This can be done like so:
在 Silverlight 3 中,现在有一个提供获取/放置像素功能的 WriteableBitmap。这可以像这样完成:
// setting a pixel example
WriteableBitmap bitmap = new WriteableBitmap(400, 200);
Color c = Colors.Purple;
bitmap.Pixels[0] = c.A << 24 | c.R << 16 | c.G << 8 | c.B;
Basically, setting a Pixel involves setting its color, and that comes by bitshifting the alpha, red, blue, green values into an integer.
基本上,设置像素涉及设置其颜色,这是通过将 alpha、红色、蓝色、绿色值位移为整数来实现的。
My question is, how would you turn an integer back to a Color? What goes in the missing spot in this example:
我的问题是,如何将整数变回颜色?在这个例子中缺失的地方是什么:
// getting a pixel example
int colorAsInt = bitmap.Pixels[0];
Color c;
// TODO:: fill in the color c from the integer ??
Thanks for any help you might have, I'm just not up on my bit shifting and I'm sure others will run into this roadblock at some point.
感谢您可能提供的任何帮助,我只是不知道我的位转移,我相信其他人会在某个时候遇到这个障碍。
回答by Nelson
Color.FromArgb(intVal)
回答by Sam Harwell
You might be looking for the ColorTranslator class, but I'm not sure it's available in SilverLight or what you need. It's absolutely a good one to be aware of though.
您可能正在寻找ColorTranslator 类,但我不确定它是否在 SilverLight 中可用或您需要什么。不过,这绝对是一个很好的注意事项。
Edit: Here was one person's suggestion(use reflection to duplicate the class, so from then on you have a converter available that people are already familiar with).
编辑:这是一个人的建议(使用反射来复制类,所以从那时起你就有了一个人们已经熟悉的转换器)。
回答by Scott Ivey
You could possibly use BitConverter.GetBytes() to convert your int to a byte array that would work with the overloads of FromArgb that silverlight has...
您可以使用BitConverter.GetBytes() 将您的 int 转换为一个字节数组,该数组将与 Silverlight 具有的 FromArgb 的重载一起使用...
Color.FromArgb(BitConverter.GetBytes(intVal));
// or if that doesn't work
var bytes = BitConverter.GetBytes(intVal);
Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);
回答by SmartyP
using the reflector i found how R,G,B are parsed in the standard .net call (not available in Silverlight):
使用反射器,我发现如何在标准 .net 调用中解析 R、G、B(在 Silverlight 中不可用):
System.Drawing.ColorTranslator.FromWin32()
from that i guessed how to get the alpha channel as well, and this does the job:
从中我猜到了如何获得 alpha 通道,这可以完成这项工作:
Color c2 = Color.FromArgb((byte)((colorAsInt >> 0x18) & 0xff),
(byte)((colorAsInt >> 0x10) & 0xff),
(byte)((colorAsInt >> 8) & 0xff),
(byte)(colorAsInt & 0xff));
回答by Bill Reiss
What has not been covered is that WriteableBitmap
uses premultipliedARGB32 so if you have a semitransparent pixel the R, G, and B values are scaled from 0 to the Alpha value.
尚未涉及的是WriteableBitmap
使用预乘ARGB32,因此如果您有一个半透明像素,则 R、G 和 B 值将从 0 缩放到 Alpha 值。
To get the Color value back, you need to do the opposite and scale it back up to 0 to 255. Something as shown below.
要恢复颜色值,您需要执行相反的操作并将其缩放到 0 到 255。如下所示。
r = (byte)(r * (255d / alpha))
回答by FFire
public static Color ToColor(this uint argb)
{
return Color.FromArgb((byte)((argb & -16777216) >> 0x18),
(byte)((argb & 0xff0000) >> 0x10),
(byte)((argb & 0xff00) >> 8),
(byte)(argb & 0xff));
}
and using:
并使用:
Color c = colorAsInt.ToColor()
回答by Judah Gabriel Himango
I think something like this should work:
我认为这样的事情应该有效:
public byte[] GetPixelBytes(WriteableBitmap bitmap)
{
int[] pixels = bitmap.Pixels;
int length = pixels.Length * 4;
byte[] result = new byte[length]; // ARGB
Buffer.BlockCopy(pixels, 0, result, 0, length);
return result;
}
Once you have the bytes, getting colors is easy with any of the various Color APIs.
获得字节后,使用各种颜色 API 中的任何一种都可以轻松获取颜色。