110,546
社区成员
发帖
与我相关
我的任务
分享
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 210; i <= 850; i += 1)
{
for (int j = 320; j <= 500; j += 1)
{
Point ap = new Point(i, j);
IntPtr ahdc = GetDC(new IntPtr(0));
int ac = GetPixel(ahdc, ap);
if (ac == 255)
{
label1.text = "Error"
break;
}
}
}
}
DateTime b = DateTime.Now;
Rectangle rect = new Rectangle();
rect.X = 210;
rect.Y = 320;
rect.Width = 850 - 210;
rect.Height = 500 - 320;
using (Bitmap bmp = new Bitmap(rect.Width, rect.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(rect.X, rect.Y, 0, 0, new Size(rect.Width, rect.Height));
}
for (int i = 0; i < rect.Width; i++)
{
for (int j = 0; j < rect.Height; j++)
{
Color c = bmp.GetPixel(i, j);
if (c == Color.White)
{
throw new Exception("颜色错误。");
}
}
}
}
double d = (DateTime.Now - b).TotalMilliseconds;
MessageBox.Show(string.Format("检测结束,共用时:{0}毫秒。", d));
不用调用Win32 API的,.Net都封装好了。
private volatile bool _flag = false;
private void timer1_Tick(object sender, EventArgs e)
{
if(_flag) { return; }
_flag = true;
bool bBreak = false;
IntPtr ahdc = GetDC(new IntPtr(0));
for (int i = 210; i <= 850; i += 1)
{
if(bBreak) break;
for (int j = 320; j <= 500; j += 1)
{
if(bBreak) break;
Point ap = new Point(i, j);
int ac = GetPixel(ahdc, ap);
if (ac == 255)
{
label1.text = "Error"
bBreak = true;
}
}
}
ReleaseDC(ahdc);
_flag = false;
}