自动截取桌面并按照所截图的小图找到桌面图且点击
作者:互联网
简单设计界面
点击picture下方的按钮进入桌面截图
其中截图需要定义的方法和属性:
private ScreenForm sf = new ScreenForm();
private Thread thread;
private Bitmap curBitmap;
private System.Threading.Timer _timer;
private int cur;
public Form2()
{
InitializeComponent();
}
[Flags]
enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[DllImport("user32.dll")]
public static extern IntPtr CreateDC(
string lpszDriver,
string lpszDevice,
string lpszzouput,
IntPtr lpdate
);
[DllImport("user32.dll")]
public static extern bool BitBlt(
int hdcDest,
int x,
int y,
int widht,
int htght,
IntPtr hdcsrc,
int xsrc,
int ysrc,
System.Int32 dw
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // 目标 DC的句柄
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hdcSrc, // 源DC的句柄
int nXSrc,
int nYSrc,
System.Int32 dwRop // 光栅的处理数值
);
private void button1_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized; //窗体状态默认大小
this.Activate();
_timer = new System.Threading.Timer(c => DisDataPlay(), null, 0, 1000);
//var therd = new Thread(new ThreadStart(DisDataPlay));
//thread = new Thread(new ThreadStart(DisDataPlay));
//thread.Name = "武功第一打手";
//thread.Start();
}
public void DisDataPlay()
{
if (checkBox1.Checked)
{
var val=Convert.ToInt32(textBox1.Text);
textBox1.Text = (val - 1).ToString();
if (val<=0)
{
using (Process process = new Process())
{
process.StartInfo.FileName = "cmd.exe";//调用cmd.exe程序
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;//重定向标准输入
process.StartInfo.RedirectStandardOutput = true;//重定向标准输出
process.StartInfo.RedirectStandardError = true;//重定向标准出错
process.StartInfo.CreateNoWindow = true;//不显示黑窗口
process.Start();//开始调用执行
process.StandardInput.WriteLine("shutdown -s -f -t 0&exit");
//process.StandardInput.WriteLine(str + "&exit");//标准输入str + "&exit",相等于在cmd黑窗口输入str + "&exit"
process.StandardInput.AutoFlush = true;//刷新缓冲流,执行缓冲区的命令,相当于输入命令之后回车执行
process.WaitForExit();//等待退出
process.Close();//关闭进程
}
}
}
Application.DoEvents();
int screenWidth = System.Windows.Forms.SystemInformation.VirtualScreen.Width; //屏幕宽度
int screenHeight = System.Windows.Forms.SystemInformation.VirtualScreen.Height; //屏幕高度
Bitmap bmSave = new Bitmap(screenWidth, screenHeight);
Graphics g = Graphics.FromImage(bmSave);
g.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight), CopyPixelOperation.SourceCopy);
g.Dispose();
var url = Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".png";
//var dd = File.Exists(url);
//if (File.Exists(url))
//{
// File.Delete(url);
//}
try
{
//bmSave.Save(url, System.Drawing.Imaging.ImageFormat.Png);
}
catch (Exception)
{
}
//var xiaotu = Application.StartupPath + "\\112233.png";
//var data = (Bitmap)this.ptb_start.Image;
//var pingmu = SaveImage();
//Bitmap xiaotubit = Image.FromFile(xiaotu) as Bitmap;
//Bitmap pingmubit = Image.FromFile(url) as Bitmap; ;
//var data = new FindSmallImgInBig().GetImageContains(xiaotubit, pingmubit, 0);
//MessageBox.Show(data.ToString());
//var rec = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\开始.png", url, 0);
if ((Bitmap)this.ptb_start.Image != null)
{
var rec = new FindSmallImgInBig().FindPicture((Bitmap)this.ptb_start.Image, bmSave, 0);
if (rec.Count > 0)
{
Cursor.Position = rec[0]; //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.ptb_center.Image != null)
{
//var rec1 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\结束.png", url, 0);
var rec1 = new FindSmallImgInBig().FindPicture((Bitmap)this.ptb_center.Image, bmSave, 0);
if (rec1.Count > 0)
{
Cursor.Position = rec1[0]; //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.ptb_stop.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最后.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.ptb_stop.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox1.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最后.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox1.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox2.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最后.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox2.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox3.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最后.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox3.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox4.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最后.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox4.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
if ((Bitmap)this.pictureBox5.Image != null)
{
//var rec2 = new FindSmallImgInBig().FindPicture(Application.StartupPath + "\\最后.png", url, 0);
var rec2 = new FindSmallImgInBig().FindPicture((Bitmap)this.pictureBox5.Image, bmSave, 0);
if (rec2.Count > 0)
{
Cursor.Position = rec2[0]; //这里是移动鼠标的代码
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
g.Dispose();
bmSave.Dispose();
}
private void Form2_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
sf.ScreenShotOk += new EventHandler(ScreenShotOk_Click);
}
private void ScreenShotOk_Click(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(sf.End.X - sf.Start.X, sf.End.Y - sf.Start.Y);
using (Graphics g = Graphics.FromImage(bmp))
{
int w = sf.End.X - sf.Start.X;
int h = sf.End.Y - sf.Start.Y;
Rectangle destRect = new Rectangle(0, 0, w + 1, h + 1);//在画布上要显示的区域(记得像素加1)
Rectangle srcRect = new Rectangle(sf.Start.X, sf.Start.Y - 15, w + 1, h + 1);//图像上要截取的区域
g.DrawImage(curBitmap, destRect, srcRect, GraphicsUnit.Pixel);//加图像绘制到画布上
}
if(cur==1)
this.ptb_start.Image = bmp;
if (cur == 2)
this.ptb_center.Image = bmp;
if (cur == 3)
this.ptb_stop.Image = bmp;
if (cur == 4)
this.pictureBox1.Image = bmp;
if (cur == 5)
this.pictureBox2.Image = bmp;
if (cur == 6)
this.pictureBox3.Image = bmp;
if (cur == 7)
this.pictureBox4.Image = bmp;
if (cur == 8)
this.pictureBox5.Image = bmp;
this.Show();
}
private void btn_start_Click(object sender, EventArgs e)
{
cur = 1;
this.Hide();//隐藏当前
//this.ptb_start.LineHistory.Clear();//清除绘制的历史线条
//this.ptb_start.RectHistory.Clear();
this.curBitmap = GetScreen();
sf.BackgroundImage = this.curBitmap;
sf.StartPosition = FormStartPosition.Manual;//起始位置
sf.ShowDialog();
}
public Bitmap GetScreen()
{
//获取整个屏幕图像,不包括任务栏
Rectangle ScreenArea = Screen.GetWorkingArea(this);
Bitmap bmp = new Bitmap(ScreenArea.Width, ScreenArea.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen(0, 0, 0, 0, new Size(ScreenArea.Width, ScreenArea.Height));
}
return bmp;
}
private void Form2_KeyDown(object sender, KeyEventArgs e)
{
// Initialize the flag to false.
if (e.KeyCode == Keys.F5)
{
this.WindowState = FormWindowState.Minimized; //窗体状态默认大小
this.Activate();
_timer = new System.Threading.Timer(c => DisDataPlay(), null, 0, 2000);
}
if (e.KeyCode == Keys.F10)
{
_timer.Change(-1, 10);
}
}
标签:截图,int,Image,var,Bitmap,小图,rec2,new,桌面图 来源: https://www.cnblogs.com/liao-long/p/15261862.html