using System;
|
using System.Collections.Generic;
|
using System.Drawing;
|
using System.Linq;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
|
namespace DetectorUtils.Captures
|
{
|
public class CaptureAuto
|
{
|
|
|
public CaptureAuto(double frameRate, Action<Bitmap> captureCallback)
|
:this(frameRate,captureCallback,-1,-1,-1,-1)
|
{
|
}
|
|
public CaptureAuto(double frameRate, Action<Bitmap> captureCallback, Int32 startX, Int32 startY, Int32 width, Int32 height)
|
{
|
this.frameRate = frameRate;
|
this.captureCallback = captureCallback;
|
this.Status = CaptureStatus.Stop;
|
this.AreaStartX = startX;
|
this.AreaStartY = startY;
|
this.AreaWidth = width;
|
this.AreaHeight = height;
|
|
Int32 screenWidth;
|
Int32 screenHeight;
|
Utils.GetWindowSize(out screenWidth, out screenHeight);
|
if (startX >= 0 && startY >= 0 && width > 0 && height > 0 && (startX + width) <= screenWidth && (startY + height) <= screenHeight)
|
{
|
captureArea = true;
|
}
|
else
|
{
|
captureArea = false;
|
}
|
}
|
|
private CancellationTokenSource CancellationSource;//取消源
|
private double frameRate = 30d;//30帧
|
private Action<Bitmap> captureCallback;//回调
|
private DateTime time = DateTime.Now;
|
private bool captureArea = false;//是否只截取一部分画面
|
public Int32 AreaStartX=0;
|
public Int32 AreaStartY=0;
|
public Int32 AreaWidth=0;
|
public Int32 AreaHeight=0;
|
/// <summary>
|
/// 状态
|
/// </summary>
|
public CaptureStatus Status
|
{
|
get;
|
private set;
|
}
|
|
public void Start()
|
{
|
if (this.Status == CaptureStatus.Stop)
|
{
|
CancellationSource = new CancellationTokenSource();
|
ThreadPool.QueueUserWorkItem(AutoCapture, CancellationSource.Token);
|
Status = CaptureStatus.Captureing;
|
}
|
}
|
|
public void Stop()
|
{
|
if (CancellationSource != null&&Status==CaptureStatus.Captureing)
|
{
|
CancellationSource.Cancel();
|
CancellationSource = null;
|
Status = CaptureStatus.Stop;
|
}
|
}
|
|
/// <summary>
|
/// 改变大小
|
/// </summary>
|
public void Resize(Int32 width,Int32 height)
|
{
|
Int32 screenWidth = 0;
|
Int32 screenHeight = 0;
|
Utils.GetWindowSize(out screenWidth, out screenHeight);
|
if (width <= 0 || (this.AreaStartX + width) > screenWidth)
|
{
|
this.AreaWidth = screenWidth - this.AreaStartX;
|
}
|
else
|
{
|
this.AreaWidth = width;
|
}
|
|
if (height <= 0 || (this.AreaStartY + height) > screenHeight)
|
{
|
this.AreaHeight = screenHeight - this.AreaStartY;
|
}
|
else
|
{
|
this.AreaHeight = height;
|
}
|
|
}
|
|
/// <summary>
|
/// 移动到指定位置
|
/// </summary>
|
public void MoveTo(Int32 x,Int32 y)
|
{
|
Int32 screenWidth = 0;
|
Int32 screenHeight = 0;
|
Utils.GetWindowSize(out screenWidth, out screenHeight);
|
|
if (x < 0 || x > screenWidth)
|
{
|
this.AreaStartX = 0;
|
}
|
else
|
{
|
this.AreaStartX = x;
|
}
|
if (y < 0 || y > screenHeight)
|
{
|
this.AreaStartY = 0;
|
}
|
else
|
{
|
this.AreaStartY = y;
|
}
|
|
if ((this.AreaStartX + this.AreaWidth) > screenWidth)
|
{
|
this.AreaStartX = screenWidth - this.AreaWidth;
|
}
|
|
if ( (this.AreaStartY + this.AreaHeight) > screenHeight)
|
{
|
this.AreaStartY = screenHeight - this.AreaHeight;
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
/// 自动截屏
|
/// </summary>
|
/// <param name="obj"></param>
|
private void AutoCapture(object obj)
|
{
|
double d = 1000d / frameRate;
|
CancellationToken token = (CancellationToken)obj;
|
|
while (!token.IsCancellationRequested)
|
{
|
double ts = (DateTime.Now - time).TotalMilliseconds;
|
if (ts < d)
|
{
|
Thread.Sleep((Int32)(d - ts));
|
continue;
|
}
|
Bitmap bitmap = null;
|
if (captureArea)
|
{
|
bitmap = ScreenCapture.CaptureScreen(this.AreaStartX, this.AreaStartY, this.AreaWidth, this.AreaHeight);
|
}
|
else
|
{
|
bitmap = ScreenCapture.CaptureScreen();
|
}
|
if (this.captureCallback != null)
|
{
|
this.captureCallback(bitmap);
|
}
|
time = DateTime.Now;
|
}
|
|
}
|
|
public enum CaptureStatus : byte
|
{
|
Stop,//停止状态
|
Captureing//截屏状态
|
}
|
}
|
}
|