您的位置:程序门 -> .net技术 -> c#



已知当前点的坐标,获得该点的颜色?????????????


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


已知当前点的坐标,获得该点的颜色?????????????[已结贴,结贴人:aaajedll_roy]
发表于:2007-01-25 15:06:27 楼主
在我的form上有一点是蓝色的,我想点击button使得label上显示该点的颜色即 "bule "
发表于:2007-01-25 15:39:591楼 得分:3
label1.backcolor   =   color.blue;
发表于:2007-01-25 15:45:032楼 得分:3
可能要用api了,getpixel:
the   getpixel   function   retrieves   the   red,   green,   blue   (rgb)   color   value   of   the   pixel   at   the   specified   coordinates.  


colorref   getpixel(
    hdc   hdc,         //   handle   to   dc
    int   nxpos,     //   x-coordinate   of   pixel
    int   nypos       //   y-coordinate   of   pixel
);
发表于:2007-01-25 15:46:403楼 得分:0
我是假设这点是蓝的,我们是不知道这点的颜色的,虽然眼睛可以辨别,但是代码里面不能定死了。楼上这种方法和坐标的xy轴的值不是没有关系了吗。如果form上有很多不同颜色的点的话,在循环里面写死不就输出同一种颜色了
发表于:2007-01-25 15:49:124楼 得分:0
上一条不是和hbxtlhx讨论的,hbxtlhx的方法能不能再详细点,谢谢
发表于:2007-01-25 16:19:285楼 得分:2
你可以参考下如下的代码:
[dllimport( "gdi32.dll ",   charset   =   charset.auto,   exactspelling   =   true)]
private   static   extern   int   getpixel(intptr   hdc,   int   nxpos,   int   nypos);
[dllimport( "user32.dll ",   entrypoint   =   "getdcex ",   charset   =   charset.auto,   exactspelling   =   true)]
private   static   extern   intptr   intgetdcex(intptr   hwnd,   intptr   hrgnclip,   int   flags);

private   void   button2_click(object   sender,   eventargs   e)
{
point   clientpoint   =   this.pointtoclient(control.mouseposition);
intptr   dc   =   intgetdcex(this.handle,   intptr.zero,   0x402);
this.label1.forecolor   =   color.fromargb(getpixel(dc,   clientpoint.x,   clientpoint.y));
}
发表于:2007-01-25 16:25:036楼 得分:2
上面的方法有点问题,颜色不太对,你可以使用如下的代码来操作:
[dllimport( "gdi32.dll ",   charset   =   charset.auto,   exactspelling   =   true)]
private   static   extern   int   getpixel(intptr   hdc,   int   nxpos,   int   nypos);
[dllimport( "user32.dll ",   entrypoint   =   "getdcex ",   charset   =   charset.auto,   exactspelling   =   true)]
private   static   extern   intptr   intgetdcex(intptr   hwnd,   intptr   hrgnclip,   int   flags);
protected   override   void   onclick(eventargs   e)
{
point   clientpoint   =   this.pointtoclient(control.mouseposition);
intptr   dc   =   intgetdcex(this.handle,   intptr.zero,   0x402);
int   intcolor   =   getpixel(dc,   clientpoint.x,   clientpoint.y);
int   num1   =   intcolor   &   0xff;
int   num2   =   (intcolor   > >   8)   &   0xff;
int   num3   =   (intcolor   > >   0x10)   &   0xff;


this.label1.forecolor   =   color.fromargb(num1,   num2,   num3);
base.onclick(e);
}


快速检索

最新资讯
热门点击