WriteableBitmap 严重失败,像素数组非常不准确
我已经尝试了几个小时,但一直无法解决这个问题。
我有一个 UserControl,即 800x369,它简单地包含一条形成世界地图的路径。 我将其放在横向页面上,然后将其渲染为 WriteableBitmap。 然后,我运行转换,将一维像素数组转换为二维整数数组。 然后,为了检查转换,我连接了自定义控件的单击命令,以使用相对于新创建的数组中的自定义控件的 Point.X 和 Point.Y。 我的逻辑是这样的:
wb = new WriteableBitmap(worldMap, new TranslateTransform());
wb.Invalidate();
intTest = wb.Pixels.To2DArray(wb.PixelWidth);
我的转换逻辑是这样的:
public static int[,] To2DArray(this int[] arr,int rowLength)
{
int[,] output = new int[rowLength, arr.Length / rowLength];
if (arr.Length % rowLength != 0) throw new IndexOutOfRangeException();
for (int i = 0; i < arr.Length; i++)
{
output[i % rowLength, i / rowLength] = arr[i];
}
return output;
}
现在,当我进行检查时,我得到了完全奇怪的结果:显然所有像素的值要么是 -1 要么是 0,并且这些值是 完全独立于原始颜色。
仅供后人参考:这是我的检查代码:
private void Check(object sender, MouseButtonEventArgs e)
{
Point click = e.GetPosition(worldMap);
ChangeNotification(intTest[(int)click.X,(int)click.Y].ToString());
}
结果显示与 WriteableBitmap 渲染到其中的路径绝对没有相关性。路径充满纯白色。
到底是怎么回事?我已经尝试了几个小时但没有运气。请注意,这是阻止我提交第一个 WP7 应用程序的主要问题。有什么指导吗?
I have tried, literally for hours, and I have not been able to budge this problem.
I have a UserControl, that is 800x369, and it contains, simply, a path that forms a worldmap.
I put this on a landscape page, then I render it into a WriteableBitmap.
I then run a conversion to turn the 1d Pixels array into a 2d array of integers.
Then, to check the conversion, I wire up the custom control's click command to use the Point.X and Point.Y relative to the custom control in the newly created array.
My logic is thus:
wb = new WriteableBitmap(worldMap, new TranslateTransform());
wb.Invalidate();
intTest = wb.Pixels.To2DArray(wb.PixelWidth);
My conversion logic is as such:
public static int[,] To2DArray(this int[] arr,int rowLength)
{
int[,] output = new int[rowLength, arr.Length / rowLength];
if (arr.Length % rowLength != 0) throw new IndexOutOfRangeException();
for (int i = 0; i < arr.Length; i++)
{
output[i % rowLength, i / rowLength] = arr[i];
}
return output;
}
Now, when I do the checking, I get completely and utterly strange results: apparently all pixels are either at values of -1 or 0, and these values are completely independent of the original colours.
Just for posterity: here's my checking code:
private void Check(object sender, MouseButtonEventArgs e)
{
Point click = e.GetPosition(worldMap);
ChangeNotification(intTest[(int)click.X,(int)click.Y].ToString());
}
The result show absolutely no correlation to the path that the WriteableBitmap has rendered into it. The path has a fill of solid white.
What the heck is going on? I've tried for hours with no luck. Please, this is the major problem stopping me from submitting my first WP7 app. Any guidance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码工作正常,Silverlight 使用预乘 ARGB,因此对于透明,每个 RGB 值将乘以 0,最终结果将为 0。对于白色,您将获得 4 个 255 字节,由于整数的表示方式,该字节为 -1 。您可以使用 BitConverter 类从 int 获取字节,但我建议使用具有 Get/SetPixel 方法的 WriteableBitmapEx并为您处理预乘。
如果您只是查找十六进制值:
ChangeNotification(intTest[(int)click.X,(int)click.Y].ToString("X8"));
You code works correctly, Silverlight uses pre-multiplied ARGB, so for transparent each of the RGB values will get multiplied by 0 and the final result will be 0. For white you get four 255 bytes which is -1 because of how integers are represented. You can get the bytes from an int using the BitConverter class but I'd recommend WriteableBitmapEx which has Get/SetPixel methods and handles the the pre-multiplication for you.
If you were just looking for hex values:
ChangeNotification(intTest[(int)click.X,(int)click.Y].ToString("X8"));