private Color GetPointColor(Point ColorPoint)//获取点颜色
{
Color PointColor;
// 创建显示器的DC
IntPtr hdlDisplay = CreateDC("DISPLAY", null, null, IntPtr.Zero);
// 从指定设备的句柄创建新的 Graphics 对象
Graphics gfxDisplay = Graphics.FromHdc(hdlDisplay);
// 创建只有一个象素大小的 Bitmap 对象
Bitmap bmp = new Bitmap(1, 1, gfxDisplay);
// 从指定 Image 对象创建新的 Graphics 对象
Graphics gfxBmp = Graphics.FromImage(bmp);
// 获得屏幕的句柄
IntPtr hdlScreen = gfxDisplay.GetHdc();
// 获得位图的句柄
IntPtr hdlBmp = gfxBmp.GetHdc();
// 把当前屏幕中鼠标指针所在位置的一个象素拷贝到位图中
BitBlt(hdlBmp,0, 0, 1, 1, hdlScreen, 0, 0, 13369376);
// 释放屏幕句柄
gfxDisplay.ReleaseHdc(hdlScreen);
// 释放位图句柄
gfxBmp.ReleaseHdc(hdlBmp);
PointColor = bmp.GetPixel(0, 0); // 获取像素的颜色
labelRGB.Text = "0x" + PointColor.ToArgb().ToString("x").ToUpper();
gfxDisplay.Dispose();
gfxBmp.Dispose();
bmp.Dispose(); // 释放 bmp 所使用的资源
return PointColor;
}
转载于:https://www.cnblogs.com/yxbsmx/articles/1280322.html