功能导致屏幕闪现冻结
我有一个并不那么复杂的函数,但由于某种原因,每次我调用它时,它都会冻结屏幕大约四分之一秒。该功能基于计时器,每秒部署一次,因此可能会让用户感到非常烦人。
该函数基本上获取屏幕的屏幕截图,通过比较 10 个像素来检查它是否正在寻找的屏幕,如果是屏幕则返回 true,否则返回 false:
ClassName.CheckScreen(CaptureScreen(),ClassName.Pxarr1); //Capture screen isn't the problem. It gave me no freezes in an endless loop.
这就是类:
class ClassName
{
public static Pixel[] Pxarr1 = new[]
{
new Pixel(Color.FromArgb(255, 204, 204, 170), new Point(15, 145)),
new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(20, 460)),
new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(20, 545)),
new Pixel(Color.FromArgb(255, 204, 187, 170), new Point(15, 150)),
new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(22, 190)),
new Pixel(Color.FromArgb(255, 204, 204, 187), new Point(25, 540)),
new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(22, 61)),
new Pixel(Color.FromArgb(255, 221, 204, 170), new Point(23, 563)),
new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(23, 47)),
new Pixel(Color.FromArgb(255, 204, 204, 187), new Point(23, 463)),
};
public static Pixel[] Pxarr2 = new[]
{
new Pixel(Color.FromArgb(255, 221, 255, 119), new Point(80, 120)),
new Pixel(Color.FromArgb(255, 51, 119, 221), new Point(180, 525)),
new Pixel(Color.FromArgb(255, 204, 170, 85), new Point(630, 455)),
new Pixel(Color.FromArgb(255, 85, 153, 17), new Point(707, 177)),
new Pixel(Color.FromArgb(255, 255, 153, 34), new Point(520, 440)),
new Pixel(Color.FromArgb(255, 255, 238, 51), new Point(150, 325)),
new Pixel(Color.FromArgb(255, 0, 85, 255), new Point(455, 70)),
new Pixel(Color.FromArgb(255, 255, 221, 51), new Point(685, 285)),
new Pixel(Color.FromArgb(255, 17, 17, 17), new Point(547, 369)),
new Pixel(Color.FromArgb(255, 170, 170, 136), new Point(500, 545)),
};
public static Pixel[] Pxarr3 = new[]
{
new Pixel(Color.FromArgb(255, 238, 238, 238), new Point(353, 223)),
new Pixel(Color.FromArgb(255, 28, 33, 49), new Point(428, 198)),
new Pixel(Color.FromArgb(255, 85, 85, 85), new Point(462, 314)),
new Pixel(Color.FromArgb(255, 221, 221, 238), new Point(450, 450)),
new Pixel(Color.FromArgb(255, 102, 102, 102), new Point(384, 349)),
new Pixel(Color.FromArgb(255, 204, 204, 204), new Point(406, 248)),
new Pixel(Color.FromArgb(255, 221, 221, 221), new Point(464, 453)),
new Pixel(Color.FromArgb(255, 255, 204, 17), new Point(413, 198)),
new Pixel(Color.FromArgb(255, 204, 204, 204), new Point(343, 447)),
new Pixel(Color.FromArgb(255, 255, 255, 255), new Point(403, 457)),
};
public static Pixel[] Pxarr4 = new[]
{
new Pixel(Color.FromArgb(255, 204, 170, 136), new Point(120, 227)),
new Pixel(Color.FromArgb(255, 221, 187, 153), new Point(502, 170)),
new Pixel(Color.FromArgb(255, 119, 85, 34), new Point(692, 243)),
new Pixel(Color.FromArgb(255, 238, 221, 187), new Point(211, 169)),
new Pixel(Color.FromArgb(255, 187, 170, 136), new Point(272, 238)),
new Pixel(Color.FromArgb(255, 170, 153, 119), new Point(696, 64)),
new Pixel(Color.FromArgb(255, 136, 85, 17), new Point(306, 242)),
new Pixel(Color.FromArgb(255, 187, 170, 119), new Point(115, 236)),
new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(310, 183)),
new Pixel(Color.FromArgb(255, 153, 102, 34), new Point(647, 245)),
};
public static Pixel[] Pxarr5 = new[]
{
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(376, 150)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(376, 350)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(506, 150)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(506, 350)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(246, 150)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(246, 350))
};
public static bool CheckScreen(Bitmap img, Pixel[] samples)
{
int verifiedpixels = 0;
for (int i = 0; i < samples.Length; i++)
{
Color c = img.GetPixel(samples[i].Location.X, samples[i].Location.Y);
if (c == samples[i].Color)
verifiedpixels++;
}
return verifiedpixels >= (samples.Length * 0.6);
}
}
注意:100 个循环 for 语句的速度是CaptureScreen() 与使用文件中的图像相比。 (4 秒与 2 秒)
但是,我仍然不明白为什么这个函数会导致冻结,即使使用文件中的图像也是如此。
I have a function which isn't so complicated, but for some reason, each time I call it, it freezes the screen for like quarter of a second. This function is on a timer and is deployed every second so it can get pretty annoying to the user.
The function basically gets a screen shot of the screen, checks if the screen that it's looking for by comparing 10 pixels and returns true if that's the screen or false otherwise :
ClassName.CheckScreen(CaptureScreen(),ClassName.Pxarr1); //Capture screen isn't the problem. It gave me no freezes in an endless loop.
Thats the class :
class ClassName
{
public static Pixel[] Pxarr1 = new[]
{
new Pixel(Color.FromArgb(255, 204, 204, 170), new Point(15, 145)),
new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(20, 460)),
new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(20, 545)),
new Pixel(Color.FromArgb(255, 204, 187, 170), new Point(15, 150)),
new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(22, 190)),
new Pixel(Color.FromArgb(255, 204, 204, 187), new Point(25, 540)),
new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(22, 61)),
new Pixel(Color.FromArgb(255, 221, 204, 170), new Point(23, 563)),
new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(23, 47)),
new Pixel(Color.FromArgb(255, 204, 204, 187), new Point(23, 463)),
};
public static Pixel[] Pxarr2 = new[]
{
new Pixel(Color.FromArgb(255, 221, 255, 119), new Point(80, 120)),
new Pixel(Color.FromArgb(255, 51, 119, 221), new Point(180, 525)),
new Pixel(Color.FromArgb(255, 204, 170, 85), new Point(630, 455)),
new Pixel(Color.FromArgb(255, 85, 153, 17), new Point(707, 177)),
new Pixel(Color.FromArgb(255, 255, 153, 34), new Point(520, 440)),
new Pixel(Color.FromArgb(255, 255, 238, 51), new Point(150, 325)),
new Pixel(Color.FromArgb(255, 0, 85, 255), new Point(455, 70)),
new Pixel(Color.FromArgb(255, 255, 221, 51), new Point(685, 285)),
new Pixel(Color.FromArgb(255, 17, 17, 17), new Point(547, 369)),
new Pixel(Color.FromArgb(255, 170, 170, 136), new Point(500, 545)),
};
public static Pixel[] Pxarr3 = new[]
{
new Pixel(Color.FromArgb(255, 238, 238, 238), new Point(353, 223)),
new Pixel(Color.FromArgb(255, 28, 33, 49), new Point(428, 198)),
new Pixel(Color.FromArgb(255, 85, 85, 85), new Point(462, 314)),
new Pixel(Color.FromArgb(255, 221, 221, 238), new Point(450, 450)),
new Pixel(Color.FromArgb(255, 102, 102, 102), new Point(384, 349)),
new Pixel(Color.FromArgb(255, 204, 204, 204), new Point(406, 248)),
new Pixel(Color.FromArgb(255, 221, 221, 221), new Point(464, 453)),
new Pixel(Color.FromArgb(255, 255, 204, 17), new Point(413, 198)),
new Pixel(Color.FromArgb(255, 204, 204, 204), new Point(343, 447)),
new Pixel(Color.FromArgb(255, 255, 255, 255), new Point(403, 457)),
};
public static Pixel[] Pxarr4 = new[]
{
new Pixel(Color.FromArgb(255, 204, 170, 136), new Point(120, 227)),
new Pixel(Color.FromArgb(255, 221, 187, 153), new Point(502, 170)),
new Pixel(Color.FromArgb(255, 119, 85, 34), new Point(692, 243)),
new Pixel(Color.FromArgb(255, 238, 221, 187), new Point(211, 169)),
new Pixel(Color.FromArgb(255, 187, 170, 136), new Point(272, 238)),
new Pixel(Color.FromArgb(255, 170, 153, 119), new Point(696, 64)),
new Pixel(Color.FromArgb(255, 136, 85, 17), new Point(306, 242)),
new Pixel(Color.FromArgb(255, 187, 170, 119), new Point(115, 236)),
new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(310, 183)),
new Pixel(Color.FromArgb(255, 153, 102, 34), new Point(647, 245)),
};
public static Pixel[] Pxarr5 = new[]
{
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(376, 150)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(376, 350)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(506, 150)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(506, 350)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(246, 150)),
new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(246, 350))
};
public static bool CheckScreen(Bitmap img, Pixel[] samples)
{
int verifiedpixels = 0;
for (int i = 0; i < samples.Length; i++)
{
Color c = img.GetPixel(samples[i].Location.X, samples[i].Location.Y);
if (c == samples[i].Color)
verifiedpixels++;
}
return verifiedpixels >= (samples.Length * 0.6);
}
}
Note : A 100 loop for statement was twice as slow with CaptureScreen() than with an image from a file.
(4 seconds vs 2 seconds)
But still, I don't understand why this function casuses any freezing, even with an image from a file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是计时器,因此它不应冻结 UI,除非您使用 System.Windows.Forms.Timer 它将在 UI 线程上执行回调。而是使用 System.Timers.Timer 或 System.Threading.Timer。
System.Timers.Timer 示例:
private System.Timers.Timer _checkPixelsTimer = new System.Timers.Timer();
编辑:更新代码以预览如何在 UI 线程调用函数。
Since you are using a timer, it should not freeze the UI, unless you are using System.Windows.Forms.Timer which will execute its call back on the UI thread. Instead use System.Timers.Timer or System.Threading.Timer.
Example for
System.Timers.Timer
:private System.Timers.Timer _checkPixelsTimer = new System.Timers.Timer();
Edit: update the code to preview how to call a function at the UI thread.
GetPixel() 和 SetPixel() 是出了名的慢。
使用替代方法,例如 LockBits()/UnlockBits() 技术。请参阅在 C# 中快速使用位图
GetPixel() and SetPixel() are notoriously slow.
Use an alternative method such as the LockBits()/UnlockBits() technique. See Fast work with Bitmaps in C#