Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Being able to capture part of the screen (Windows, UIItems, etc.) #260

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/TestStack.White.UITests/WhiteTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected void RunTest(Action testAction, params WindowsFramework[] runFor)
{
path2 = testAction.Method.Name + ".png";
var filename = Path.Combine(screenshotDir, path2);
new ScreenCapture().CaptureScreenShot().Save(filename, ImageFormat.Png);
Desktop.CaptureScreenshot().Save(filename, ImageFormat.Png);
Trace.WriteLine(string.Format("Screenshot taken: {0}", filename));
}
catch (Exception)
Expand Down
25 changes: 24 additions & 1 deletion src/TestStack.White/Desktop.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows;
using System.Windows.Automation;
using TestStack.White.AutomationElementSearch;
using TestStack.White.Factory;
Expand Down Expand Up @@ -63,7 +64,29 @@ public virtual List<Window> Windows()
public static Bitmap CaptureScreenshot()
{
var screenCapture = new ScreenCapture();
return screenCapture.CaptureScreenShot();
return screenCapture.CaptureDesktop();
}

/// <summary>
/// Captures a screenshot of the provided boundary, and returns the bitmap
/// </summary>
/// <param name="bounds">Screen rectangle to capture</param>
public static Bitmap CaptureScreenshot(Rect bounds)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An method on window to expose this would be quite cool

{
var screenCapture = new ScreenCapture();
return screenCapture.CaptureArea(bounds);
}

/// <summary>
/// Takes a screenshot of the provided boundary, and saves it to disk
/// </summary>
/// <param name="bounds"></param>
/// <param name="filename">The fullname of the file (including extension)</param>
/// <param name="imageFormat"></param>
public static void TakeScreenshot(Rect bounds, string filename, ImageFormat imageFormat)
{
var bitmap = CaptureScreenshot(bounds);
bitmap.Save(filename, imageFormat);
}

/// <summary>
Expand Down
25 changes: 22 additions & 3 deletions src/TestStack.White/ScreenCapture.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows;
using TestStack.White.SystemExtensions;
using TestStack.White.UIItems.WindowItems;
using Size = System.Drawing.Size;

namespace TestStack.White
{
/// <summary>
/// Provides functions to capture the entire screen, or a particular window, and save it to a file.
/// </summary>
public class ScreenCapture
internal class ScreenCapture
{
// This code is a modified version of many similar classes along the same lines. There is no one source I can credit with this class

Expand All @@ -30,9 +35,10 @@ public class ScreenCapture
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr ptr);

public virtual Bitmap CaptureScreenShot()
public virtual Bitmap CaptureDesktop()
{
var sz = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size;
var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
var sz = bounds.Size;
var hDesk = GetDesktopWindow();
var hSrce = GetWindowDC(hDesk);
var hDest = CreateCompatibleDC(hSrce);
Expand All @@ -45,6 +51,19 @@ public virtual Bitmap CaptureScreenShot()
DeleteDC(hDest);
ReleaseDC(hDesk, hSrce);

return bmp;
}

public virtual Bitmap CaptureArea(Rect rect)
{
var rectangle = rect.ToRectangle();
var width = rectangle.Right - rectangle.Left;
var height = rectangle.Bottom - rectangle.Top;
var bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
var graphics = Graphics.FromImage(bmp);

graphics.CopyFromScreen(rectangle.Left, rectangle.Top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

return bmp;
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/TestStack.White/SystemExtensions/RectangleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Drawing;
using System.Windows;

namespace TestStack.White.SystemExtensions
{
public static class RectangleExtensions
{
public static Rect ToRect(this Rectangle rectangle)
{
return new Rect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
}

public static Rectangle ToRectangle(this Rect rect)
{
return new Rectangle((int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);
}
}
}
1 change: 1 addition & 0 deletions src/TestStack.White/TestStack.White.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<Compile Include="ScreenMap\WindowsAutomationTypesSurrogates.cs" />
<Compile Include="Span.cs" />
<Compile Include="SystemExtensions\DoubleX.cs" />
<Compile Include="SystemExtensions\RectangleExtensions.cs" />
<Compile Include="SystemExtensions\TypeExtensions.cs" />
<Compile Include="UIA\AutomationElementCollectionX.cs" />
<Compile Include="UIA\AutomationElementX.cs" />
Expand Down