Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Extracted Default ScreenGrabber
Browse files Browse the repository at this point in the history
  • Loading branch information
sriv committed Mar 18, 2016
1 parent a35e780 commit 77d16bc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
28 changes: 28 additions & 0 deletions Runner/DefaultScreenGrabber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using Gauge.CSharp.Lib;
using Google.ProtocolBuffers;

namespace Gauge.CSharp.Runner
{
public class DefaultScreenGrabber : IScreenGrabber
{
public byte[] TakeScreenShot()
{
var bounds = Screen.GetBounds(Point.Empty);
using (var bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (var g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Png);
var takeScreenshot = ByteString.CopyFrom(memoryStream.ToArray());
return takeScreenshot.ToByteArray();
}
}
}
}
1 change: 1 addition & 0 deletions Runner/Gauge.CSharp.Runner.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Compile Include="AssemblyWrapper.cs" />
<Compile Include="ClassInstanceManager.cs" />
<Compile Include="Converters\IParamConverter.cs" />
<Compile Include="DefaultScreenGrabber.cs" />
<Compile Include="ErrorCodeAggregator.cs" />
<Compile Include="Exceptions\NotAValidGaugeProjectException.cs" />
<Compile Include="ExecutionResult.cs" />
Expand Down
29 changes: 2 additions & 27 deletions Runner/MethodExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Gauge.CSharp.Runner.Converters;
using Gauge.Messages;
using Google.ProtocolBuffers;
Expand Down Expand Up @@ -80,29 +76,8 @@ public IEnumerable<string> GetAllPendingMessages()

private ByteString TakeScreenshot()
{
try
{
byte[] screenShotBytes;
if (_sandbox.TryScreenCapture(out screenShotBytes))
return ByteString.CopyFrom(screenShotBytes);

var bounds = Screen.GetBounds(Point.Empty);
using (var bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (var g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, ImageFormat.Png);
var takeScreenshot = ByteString.CopyFrom(memoryStream.ToArray());
return takeScreenshot;
}
}
catch
{
return ByteString.Empty;
}
byte[] screenShotBytes;
return _sandbox.TryScreenCapture(out screenShotBytes) ? ByteString.CopyFrom(screenShotBytes) : ByteString.Empty;
}

[DebuggerHidden]
Expand Down
16 changes: 7 additions & 9 deletions Runner/Sandbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,12 @@ public IEnumerable<string> GetStepTexts(MethodInfo stepMethod)

public bool TryScreenCapture(out byte[] screenShotBytes)
{
if (ScreenGrabberType != null)
var screenCaptureMethod = ScreenGrabberType.GetMethod("TakeScreenShot");
var instance = Activator.CreateInstance(ScreenGrabberType);
if (instance != null)
{
var screenCaptureMethod = ScreenGrabberType.GetMethod("TakeScreenShot");
var instance = Activator.CreateInstance(ScreenGrabberType);
if (instance != null)
{
screenShotBytes = screenCaptureMethod.Invoke(instance, null) as byte[];
return true;
}
screenShotBytes = screenCaptureMethod.Invoke(instance, null) as byte[];
return true;
}
screenShotBytes = null;
return false;
Expand Down Expand Up @@ -149,7 +146,8 @@ private void ScanCustomScreenGrabber()
}
else
{
Logger.Debug("No implementation of IScreenGrabber found.");
Logger.Debug("No implementation of IScreenGrabber found. Using DefaultScreenGrabber");
ScreenGrabberType = typeof (DefaultScreenGrabber);
}
}

Expand Down

0 comments on commit 77d16bc

Please sign in to comment.