在Point获取Canvas颜色

我先解释一下我要做的事情。 我正在尝试创建一个颜色选择器控件,就像右侧的其中一个: http : //demos.telerik.com/silverlight/Themesgenerator/但是我想自己创建它用于学习。

目前我已经在xaml中进行了某种布局,并且我使用了带有LinearGradientBrush背景的Canvas。 现在我在尝试确定哪个颜色位于特定点时卡住了。 有没有找到这个的好方法?..我想点击我的canvas并获得该特定Point的ARGB。 任何帮助,将不胜感激。

我找到了解决方案! 在这里,如果有人需要它!

[DllImport("gdi32")] private static extern int GetPixel(int hdc, int nXPos, int nYPos); [DllImport("user32")] private static extern int GetWindowDC(int hwnd); [DllImport("user32")] private static extern int ReleaseDC(int hWnd, int hDC); private static SolidColorBrush GetPixelColor(Point point) { int lDC = GetWindowDC(0); int intColor = GetPixel(lDC, (int)point.X, (int)point.Y); // Release the DC after getting the Color. ReleaseDC(0, lDC); byte a = (byte)( ( intColor >> 0x18 ) & 0xffL ); byte b = (byte)((intColor >> 0x10) & 0xffL); byte g = (byte)((intColor >> 8) & 0xffL); byte r = (byte)(intColor & 0xffL); Color color = Color.FromRgb(r, g, b); return new SolidColorBrush(color); } 

我用这种方式调用这个方法:

 SolidColorBrush solidcolor = GetPixelColor(RightColorPanel.PointToScreen(point)); Color color = Color.FromArgb(solidcolor.Color.A, solidcolor.Color.R, solidcolor.Color.G, solidcolor.Color.B); LinearGradientBrush brush = new LinearGradientBrush(); brush.StartPoint = new Point(0, 0); brush.EndPoint = new Point(1, 0); brush.GradientStops.Add(new GradientStop(Colors.White, 0.0)); brush.GradientStops.Add(new GradientStop(color, 1)); MainColorPanel.Background = brush; 

哪个point是我的RightColorPanel的特定点,我保持我的颜色! 这真的很棒!