快速查找屏幕 颜色

来源:百度知道 编辑:UC知道 时间:2024/05/30 23:07:27
这样说吧 我要实现查找屏幕上某点颜色的功能
我先设置一种颜色 然后在屏幕上查找

如果一个一个相素的比较的话太慢了 我要实现不能超过一秒就能遍历完整个屏幕

我找到段VB的代码 别人好象300多毫秒就能实现 希望看得懂VB的高手 帮我翻译成C# 可以了我再加分

这是用到的几个API
GetDIBits Lib "gdi32"
GetCurrentObject Lib "gdi32"
CopyMemory Lib "kernel32"
GetDC Lib "user32"
ReleaseDC Lib "user32"

附程序
Option Explicit Private Type BITMAPINFOHEADER '40 bytes biSize As Long biWidth As Long biHeight As Long biPlanes As Integer biBitCount As Integer biCompression As Long biSizeImage As Long biXPelsPerMeter As Long biYPelsPerMeter As Long biClrUsed As Long biClrImportant As Long End Type Private Type RGBQUAD rgbBlue As Byte rgbGreen As Byte rgbRed As Byte rgbReserved As Byte End Type Private Type BITMAPINFO bmiHeader As BITMAPINFOHEADER bmiColors As RGBQUAD End Type Private Type POINT x As Integer y As Integer End Type Private Const DIB_RGB_COLORS As Long = &H0& Private Const BI_RGB As Long

如果一个一个相素的比较的话太慢了 我要实现不能超过一秒就能遍历完整个屏幕,你可以把屏幕上所有的点分块,然后new几十个线程一起去找,这样就没问题了,继然是调用windows api,但速度应该除了汇编再快不了多少了。不过你机器得受得了。。

获取某点颜色

[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("user32.dll")]
static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);

[DllImport("gdi32.dll")]
static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
static public System.Drawing.Color GetPixelColor(int x, int y)
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF),
(int)(pixel & 0x0000FF00) >> 8,
(int)(pixel & 0x00FF0000) >> 16);
return color;
}

Option Explicit