我正在尝试获取特定位置的像素颜色(点击事件)。
我有以下代码:
private void iiiimage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Point point = e.GetPosition(iiiimage);
ApplicationTitle.Text = point.ToString();
var wb = new WriteableBitmap(iiiimage, null);
int arrayPos = (int)point.X * (int)point.Y;
PageTitle.Text = arrayPos.ToString();
int colorAsInt = wb.Pixels[arrayPos];
Color c = Color.FromArgb((byte)((colorAsInt >> 0x18) & 0xff),
(byte)((colorAsInt >> 0x10) & 0xff),
(byte)((colorAsInt >> 8) & 0xff),
(byte)(colorAsInt & 0xff));
r3ct.Fill = new SolidColorBrush(c);
}
截图显示有什么不对。白色错误(尖端)的位置为蓝色,但代码返回位于图像顶部某处的像素(红色)。
我做错了什么?
亲切的问候, 尼尔斯
答案 0 :(得分:1)
尝试进行以下修改。我不知道这是否适用于WP7。我认为应该考虑 ActualWidth 和 ActualHeight ,因为您需要将以单位表示的坐标转换为像素。
private void iiiimage_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
Point point = e.GetPosition(iiiimage);
ApplicationTitle.Text = point.ToString();
var wb = new WriteableBitmap(iiiimage, null);
int x, y;
x = (int)(wb.PixelWidth * point.X / iiiimage.ActualWidth);
y = (int)(wb.PixelHeight * point.Y / iiiimage.ActualHeight);
int arrayPos = y * wb.PixelWidth + x;
PageTitle.Text = String.Format("({0} ; {1}) --> {2}", x, y, arrayPos);
int colorAsInt = wb.Pixels[arrayPos];
Color c = Color.FromArgb((byte)((colorAsInt >> 0x18) & 0xff),
(byte)((colorAsInt >> 0x10) & 0xff),
(byte)((colorAsInt >> 8) & 0xff),
(byte)(colorAsInt & 0xff));
r3ct.Fill = new SolidColorBrush(c);
}