实例说明
在处理图片或者制作网页过程中,有时需要获取某个点的颜色值,传统的方法是将屏幕抓取下来保存成图片,然后使用Photoshop等软件打开此图片获取颜色值,显然这样做过于繁琐。因此,开发出屏幕颜色拾取器用于获取鼠标所在位置的颜色值,实例运行结果如图:
设计思路
在开发过程中,主要设计思路是首先要获取鼠标所在的坐标,本实例通过Control类的MousePosition属性获得。然后,获取该点的RGB值。最后,通过RGB值得到相应的颜色,并将颜色显示出来。
技术要点
本实例主要通过API函数获取指定坐标处的颜色值实现对屏幕颜色的获取,用到的API函数有CreateDC函数和GetPixel函数,下面对这两个API函数进行讲解。
(1)CreateDC函数
为专门设备创建设备场景,语法格式如下:
[DllImport("gdi32.dll")]
static public extern IntPtr CreateDC(string driverName,string deviceName,string output,IntPtr lpinitData);
lpDriverName:如果此参数为DISPLAY,代表获取整个屏幕的设备场景;如果为WINSPOOL,则是访问打印驱动。
lpDeviceName:所用专门设备的名称。
lpOutput:用null值给该参数。
lpInitData:DEVMODE,这个结构保存初始值。
例如,本实例中使用CreateDC函数创建场景,代码如下:
IntPtr displayDC = CreateDC("DISPLAY",null,null,IntPtr.Zero);
(2)GetPixel函数
在指定的设备场景中取得一个像素的RGB值,语法格式如下:
[DllImport("gdi32.dll")]
static public extern uint GetPixel(IntPtr hDC,int XPos,int YPos);
hdc:一个设备场景的句柄。
x,y:逻辑坐标中要检查的点。
例如,本实例中使用GetPixel函数获取鼠标所在坐标的RGB值,代码如下:
uint colorref=GetPixel(displayDC,screenPoint.X,screenPoint.Y);
开发步骤
(1)新建一个Windows应用程序,将其命名为GetColor,默认窗体为Form1。
(2)Form1窗体主要用到的控件及说明如表所示。
表 Form1窗体主要用到的控件及说明
控件名称 | 属性设置 | 说 明 |
dataGridView1 | 无 | 显示打印的数据 |
textBox2 | Enabled属性设置为false | 输入打印的页码范围 |
radioButton1 | Checked属性设置为true | 全部打印 |
radioButton2 | 无 | 打印指定的页码 |
button2 | Text属性设置为“打印” | 指定打印操作 |
printPreviewDialog1 | Document属性设置为printDocument1 | 显示打印预览对话框 |
printDocument1 | 无 | 设置打印文档 |
(3)主要程序代码。
Form1窗体的后台代码中,首先声明程序中用到的API函数和方法,代码如下:
[DllImport("gdi32.dll")]
//在指定的设备场景中取得一个像素的RGB值
static public extern uint GetPixel(IntPtr hDC,int XPos,int YPos);
[DllImport("gdi32.dll")]
//为专门设备创建设备场景
static public extern IntPtr CreateDC(string driverName,string deviceName,string output,IntPtr lpinitData);
[DllImport("gdi32.dll")]
static public extern bool DeleteDC(IntPtr DC); //删除场景
static public byte GetRValue(uint color) //获取R值的方法
{
return (byte)color;
}
static public byte GetGValue(uint color) //获取G值的方法
{
return ((byte)(((short)(color))>>8));
}
static public byte GetBValue(uint color) //获取B值的方法
{
return ((byte)((color)>>16));
}
自定义一个GetColor方法用于获取指定坐标处的颜色值,其返回值是Color类型。在此方法中分别调用了GetRValue方法、GetGValue方法和GetBValue方法获取指定坐标处的R、G、B值,代码如下:
public Color GetColor(Point screenPoint)
{
IntPtr displayDC=CreateDC("DISPLAY", null, null, IntPtr.Zero);//创建一个屏幕句柄
uint colorref=GetPixel(displayDC, screenPoint.X, screenPoint.Y); //取得鼠标所在位置的像素的RGB值
DeleteDC(displayDC); //删除场景
byte Red = GetRValue(colorref); //获取R值
byte Green = GetGValue(colorref); //获取G值
byte Blue = GetBValue(colorref); //获取B值
return Color.FromArgb(Red, Green, Blue); //返回RGB值对应的Color值
}
为了能即时获取鼠标所在坐标处的颜色值,向窗体中添加一个Timer组件。在Timer组件的Tick事件中调用GetColor方法获取指定坐标处的颜色值,代码如下:
private void timer1_Tick(object sender, EventArgs e)
{
//显示鼠标坐标
txtPoint.Text=Control.MousePosition.X.ToString()+","+Control.MousePosition.Y.ToString();
Point pt = new Point(Control.MousePosition.X, Control.MousePosition.Y); //实例化Point
Color cl = GetColor(pt); //获取鼠标坐在位置的Color值
panel1.BackColor = cl; //显示该颜色
txtRGB.Text = cl.R + "," + cl.G + "," + cl.B; //显示RGB值
txtColor.Text = ColorTranslator.ToHtml(cl).ToString(); //显示对应的网页颜色值
RegisterHotKey(Handle, 81, KeyModifiers.Ctrl, Keys.F); //注册热键
}