WPF 怎么获取 鼠标坐标的颜色RGB

jy12345678 2011-09-25 11:25:12
c# WPF 怎么获取 鼠标坐标的颜色RGB
找了半年了都找不到 请高手帮忙 写下代码
...全文
683 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaolin000 2013-08-16
  • 打赏
  • 举报
回复
谢谢 我调好了
jshi123 2013-08-16
  • 打赏
  • 举报
回复
可以用的。不过上面这段代码在64位下是错的,应该是: [DllImport("gdi32")] private static extern int GetPixel(IntPtr hdc, int nXPos,int nYPos); [DllImport("user32")] private static extern IntPtr GetWindowDC(IntPtr hwnd); [DllImport("user32")] private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
zhaolin000 2013-08-16
  • 打赏
  • 举报
回复
上面这段代码我试了 就是不行 WPF不支持GetPixel吧
zhaolin000 2013-08-16
  • 打赏
  • 举报
回复
能发一份用WPF获取鼠标坐标处颜色的代码吗,我一直弄不好935440883@qq.com谢谢
_lee_chong 2011-09-27
  • 打赏
  • 举报
回复
mark
jy12345678 2011-09-27
  • 打赏
  • 举报
回复
非常感谢!!!!!!!
jshi123 2011-09-26
  • 打赏
  • 举报
回复
GetPixcelColor要求传入的参数是屏幕坐标,要转换下:
private void Container_MouseMove(object sender, MouseEventArgs e)
{
var pos = e.GetPosition(null);
pos = this.PointToScreen(pos);
rectangle1.Fill = GetPixelColor(pos);
}
jy12345678 2011-09-26
  • 打赏
  • 举报
回复
namespace CS
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
[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 SolidColorBrush GetPixelColor(Point point)
{

int lDC = GetWindowDC( 0 );
int intColor = GetPixel( lDC, (int)point.X, (int)point.Y );
ReleaseDC( 0, lDC );
byte b = (byte)( ( intColor >> 0x10 ) & 0xffL );
byte g = (byte)( ( intColor >> 8 ) & 0xffL );
byte r = (byte)( intColor & 0xffL );
Color color = Color.FromRgb( r, g, b );

textBox1.Text =b.ToString()+" "+g.ToString()+" "+r.ToString();
return new SolidColorBrush(color);
}
public MainWindow()
{
InitializeComponent();

}

public byte b;

public static double MousePositionX
{ get; set; }

public static double MousePositionY
{
get;
set;
}

private void Container_MouseMove(object sender, MouseEventArgs e)
{
rectangle1.Fill = GetPixelColor(e.GetPosition(null));
}


}
}

这是我修改过1楼你给的连接里面的代码~~ 测试的时候获取的颜色是哪的? 感觉颜色不对!!
ChrisAK 2011-09-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jshi123 的回复:]

用P/Invoke调用GDI的GetPixel方法
参考http://sharpinsights.wordpress.com/2007/09/21/pixel-color-under-mouse/

引用楼主 jy12345678 的回复:
找了半年了都找不到

至于吗,哈哈
[/Quote]用不着P/Invoke...GDI+的CopyScreen就可以了.
jy12345678 2011-09-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jshi123 的回复:]
用P/Invoke调用GDI的GetPixel方法
[/Quote]
最好给点代码 加点备注 我是新手
想不到WPF 获取 个坐标的颜色RGB 数据都那么复杂~~

P/Invoke 我都没开始接触到。。。看你给的连接 里面的代码是 VB 还是C++的?
jshi123 2011-09-26
  • 打赏
  • 举报
回复
用P/Invoke调用GDI的GetPixel方法
参考http://sharpinsights.wordpress.com/2007/09/21/pixel-color-under-mouse/

[Quote=引用楼主 jy12345678 的回复:]
找了半年了都找不到
[/Quote]
至于吗,哈哈

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧