1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using Tango.SharedUI.Controls;
using Tango.SharedUI.Rendering;
public static class FrameworkElementExtensions
{
/// <summary>
/// Renders the element to bitmap file.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="filePath">The file path.</param>
/// <param name="format">The format.</param>
/// <param name="renderSize">Size of the render.</param>
/// <param name="quality">The quality.</param>
public static void RenderToFile(this FrameworkElement element, String filePath, ImageFormat format, Size renderSize, int quality)
{
RenderWindow renderer = new RenderWindow(element);
renderer.RenderToFileSynced(new Size(element.Width, element.Height), renderSize, filePath, format, quality, null);
}
}
pan> EventHandler<ScreenCaptureFrameReceivedEventArgs> ScreenFrameReceived;
public IScreenCaptureMethod CaptureMethod { get; set; }
public CaptureRegion CaptureRegion { get; set; }
public bool IsStarted { get; set; }
public TimeSpan Interval { get; set; }
public bool CaptureCursor { get; set; }
public bool EnableImageComparison { get; set; }
public ScreenCaptureEngine()
{
Interval = TimeSpan.FromMilliseconds(100);
CaptureMethod = new GdiScreenCapture();
CaptureRegion = new CaptureRegion(System.Windows.Forms.Screen.PrimaryScreen.Bounds);
_comparer = new ImageComparer();
EnableImageComparison = true;
}
public void Start()
{
if (_isDisposed)
{
throw new ObjectDisposedException("Screen capture engine cannot be started after disposed.");
}
if (!IsStarted)
{
IsStarted = true;
_captureThread = new Thread(CaptureThreadMethod);
_captureThread.IsBackground = true;
_captureThread.Name = "Screen Capture Thread";
_captureThread.Start();
}
}
public void Stop()
{
if (IsStarted)
{
IsStarted = false;
}
}
private void CaptureThreadMethod()
{
while (IsStarted)
{
var bitmap = CaptureMethod.GetDesktopBitmap(CaptureRegion);
if (CaptureCursor)
{
using (Graphics g = Graphics.FromImage(bitmap))
{
CursorUtils.ApplyCursor(g, bitmap, CaptureRegion.Left, CaptureRegion.Top);
}
}
if (EnableImageComparison)
{
if (_previousBitmap == null)
{
_previousBitmap = bitmap.Clone() as Bitmap;
OnScreenFrameReceived(new ScreenCaptureFrame(bitmap, null));
}
else
{
var diffBitmap = _comparer.CreateDifferenceBitmap(_previousBitmap, bitmap, Color.Transparent);
_previousBitmap.Dispose();
_previousBitmap = bitmap.Clone() as Bitmap;
OnScreenFrameReceived(new ScreenCaptureFrame(bitmap, diffBitmap));
}
}
else
{
OnScreenFrameReceived(new ScreenCaptureFrame(bitmap, null));
}
Thread.Sleep(Interval);
}
}
public void Dispose()
{
if (!_isDisposed)
{
_isDisposed = true;
Stop();
CaptureMethod?.Dispose();
}
}
protected virtual void OnScreenFrameReceived(ScreenCaptureFrame frame)
{
ScreenFrameReceived?.Invoke(this, new ScreenCaptureFrameReceivedEventArgs()
{
Frame = frame,
});
}
}
}
|