C# 检测像素的颜色,并在循环中单击该像素
使用此代码可以检测像素的颜色并单击该像素 我想在无限循环中点击登录按钮后检测像素的颜色,每次发现颜色程序都会自动播放声音! 例如,在无限循环或任何一秒钟内 最后,我希望这个操作可以重复执行C# 检测像素的颜色,并在循环中单击该像素,c#,visual-studio,screenshot,C#,Visual Studio,Screenshot,使用此代码可以检测像素的颜色并单击该像素 我想在无限循环中点击登录按钮后检测像素的颜色,每次发现颜色程序都会自动播放声音! 例如,在无限循环或任何一秒钟内 最后,我希望这个操作可以重复执行 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.IO;
using System.Media;
namespace Login_Button_Click_for_Puzzle_Pirates
{
public partial class frmMain : Form
{
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData,
int dwExtraInfo);
public enum MouseEventFlags : uint
{
LEFTDOWN = 0x00000002,
LEFTUP = 0x00000004,
MIDDLEDOWN = 0x00000020,
MIDDLEUP = 0x00000040,
MOVE = 0x00000001,
ABSOLUTE = 0x00008000,
RIGHTDOWN = 0x00000008,
RIGHTUP = 0x00000010,
WHEEL = 0x00000800,
XDOWN = 0x00000080,
XUP = 0x00000100
}
public frmMain()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
// takes a snapshot of the screen
Bitmap bmpScreenshot = Screenshot();
// makes the background of the form a screenshot of the screen
this.BackgroundImage = bmpScreenshot;
// find the login button and check if it exists
Point location;
bool success = FindBitmap(Properties.Resources.bmpLogin, bmpScreenshot, out location);
// check if it found the bitmap
if (success == false)
{
//MessageBox.Show("Couldn't find the login button");
return;
}
// move the mouse to login button
//Cursor.Position = location;
System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.Stream = Properties.Resources.Alarm;
player.Play();
// click
MouseClick();
/*
* [x] Snapshot of the whole screen
* [x] Find the login button and check if it exists
* [x] Move the mouse to login button
* [ ] Click the login button
*/
}
/// <summary>
/// Simulates a mouse click
/// </summary>
private void MouseClick()
{
mouse_event((uint)MouseEventFlags.LEFTDOWN, 0, 0, 0, 0);
Thread.Sleep((new Random()).Next(20, 30));
mouse_event((uint)MouseEventFlags.LEFTUP, 0, 0, 0, 0);
}
/// <summary>
/// Takes a snapshot of the screen
/// </summary>
/// <returns>A snapshot of the screen</returns>
private Bitmap Screenshot()
{
// this is where we will store a snapshot of the screen
Bitmap bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
// creates a graphics object so we can draw the screen in the bitmap (bmpScreenshot)
Graphics g = Graphics.FromImage(bmpScreenshot);
// copy from screen into the bitmap we created
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
// return the screenshot
return bmpScreenshot;
}
private bool FindBitmap(Bitmap bmpNeedle, Bitmap bmpHaystack, out Point location)
{
for(int outerX = 0; outerX < bmpHaystack.Width - bmpNeedle.Width; outerX++)
{
for (int outerY = 0; outerY < bmpHaystack.Height - bmpNeedle.Height; outerY++)
{
for (int innerX = 0; innerX < bmpNeedle.Width; innerX++)
{
for (int innerY = 0; innerY < bmpNeedle.Height; innerY++)
{
Color cNeedle = bmpNeedle.GetPixel(innerX, innerY);
Color cHaystack = bmpHaystack.GetPixel(innerX + outerX, innerY + outerY);
if (cNeedle.R != cHaystack.R || cNeedle.G != cHaystack.G || cNeedle.B != cHaystack.B)
{
goto notFound;
}
}
}
location = new Point(outerX, outerY);
return true;
notFound:
continue;
}
}
location = Point.Empty;
return false;
}
private void frmMain_Load(object sender, EventArgs e)
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用系统线程;
使用System.IO;
使用系统、媒体;
命名空间登录\u按钮\u单击\u查找\u谜题\u海盗
{
公共部分类名称:表单
{
[DllImport(“user32.dll”)]
静态外部无效鼠标事件(uint dwFlags、uint dx、uint dy、uint dwData、,
int dwExtraInfo);
公共枚举MouseEventFlags:uint
{
LEFTDOWN=0x00000002,
LEFTUP=0x00000004,
MIDDLEDOWN=0x00000020,
MIDDLEUP=0x00000040,
MOVE=0x00000001,
绝对值=0x00008000,
RIGHTDOWN=0x00000008,
RIGHTUP=0x00000010,
车轮=0x00000800,
XDOWN=0x00000080,
XUP=0x00000100
}
公共财政收入()
{
初始化组件();
}
私有void btnLogin\u单击(对象发送方,事件参数e)
{
//拍摄屏幕快照
位图bmpScreenshot=屏幕截图();
//使窗体的背景成为屏幕截图
this.BackgroundImage=bmpScreenshot;
//找到登录按钮并检查它是否存在
点定位;
bool success=FindBitmap(Properties.Resources.bmpLogin、bmpScreenshot、out位置);
//检查是否找到位图
if(success==false)
{
//Show(“找不到登录按钮”);
返回;
}
//将鼠标移动到登录按钮
//光标位置=位置;
System.Media.SoundPlayer player=新的System.Media.SoundPlayer();
player.Stream=Properties.Resources.Alarm;
player.Play();
//点击
鼠标点击();
/*
*[x]整个屏幕的快照
*[x]找到登录按钮并检查它是否存在
*[x]将鼠标移动到登录按钮
*[]单击登录按钮
*/
}
///
///模拟鼠标单击
///
私有无效鼠标单击()
{
鼠标事件((uint)MouseEventFlags.LEFTDOWN,0,0,0);
Sleep((new Random()).Next(20,30));
鼠标事件((uint)MouseEventFlags.LEFTUP,0,0,0,0);
}
///
///拍摄屏幕快照
///
///屏幕快照
私有位图截图()
{
//这是我们将存储屏幕快照的位置
位图bmpScreenshot=新位图(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);
//创建图形对象,以便我们可以在位图中绘制屏幕(bmpScreenshot)
Graphics g=Graphics.FromImage(bmpScreenshot);
//从屏幕复制到我们创建的位图中
g、 CopyFromScreen(0,0,0,0,Screen.PrimaryScreen.Bounds.Size);
//返回屏幕截图
返回bmpScreenshot;
}
私有布尔FindBitmap(位图bmpFeedle、位图bmpAsystack、输出点位置)
{
对于(int-outerX=0;outerX