2

是否有一个 API 可以让我在http://teststack.github.com/White/中做到这一点?我似乎找不到它。

谢谢

帕维尔

4

3 回答 3

4

我知道这是一个非常古老的帖子。但我虽然不能伤害更新它。TestStack.White 现在有这些功能:

//Takes a screenshot of the entire desktop, and saves it to disk
Desktop.TakeScreenshot("C:\\white-framework.png", System.Drawing.Imaging.ImageFormat.Png);
//Captures a screenshot of the entire desktop, and returns the bitmap
Bitmap bitmap = Desktop.CaptureScreenshot();
于 2017-02-23T08:50:01.220 回答
1

通过查看 GitHub 上的代码,它似乎没有相应的 API(也许将其添加为功能请求?)。

尽管使用Screen类和Graphics.CopyFromScreen. 此问题的答案中有一些关于如何捕获屏幕或活动窗口的示例。

于 2013-03-04T00:26:49.057 回答
0

White.Repository 项目实际上记录了您的测试流程,带有屏幕截图,但它的文档记录不是很好,还没有在 NuGet 上发布(很快就会发布)。

就我个人而言,我使用我们从一堆资源中汇总的这个类,忘记了我最初是从哪里得到它的。这捕获了模态对话和许多其他实现由于某种原因没有捕获的其他内容。

/// <summary>
/// Provides functions to capture the entire screen, or a particular window, and save it to a file.
/// </summary>
public class ScreenCapture
{
    [DllImport("gdi32.dll")]
    static extern bool BitBlt(IntPtr hdcDest, int xDest, int yDest, int wDest, int hDest, IntPtr hdcSource, int xSrc, int ySrc, CopyPixelOperation rop);
    [DllImport("user32.dll")]
    static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDc);
    [DllImport("gdi32.dll")]
    static extern IntPtr DeleteDC(IntPtr hDc);
    [DllImport("gdi32.dll")]
    static extern IntPtr DeleteObject(IntPtr hDc);
    [DllImport("gdi32.dll")]
    static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
    [DllImport("gdi32.dll")]
    static extern IntPtr CreateCompatibleDC(IntPtr hdc);
    [DllImport("gdi32.dll")]
    static extern IntPtr SelectObject(IntPtr hdc, IntPtr bmp);
    [DllImport("user32.dll")]
    public static extern IntPtr GetDesktopWindow();
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowDC(IntPtr ptr);

    public Bitmap CaptureScreenShot()
    {
        var sz = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size;
        var hDesk = GetDesktopWindow();
        var hSrce = GetWindowDC(hDesk);
        var hDest = CreateCompatibleDC(hSrce);
        var hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
        var hOldBmp = SelectObject(hDest, hBmp);
        BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
        var bmp = Image.FromHbitmap(hBmp);
        SelectObject(hDest, hOldBmp);
        DeleteObject(hBmp);
        DeleteDC(hDest);
        ReleaseDC(hDesk, hSrce);

        return bmp;
    }
}

然后消费

var sc = new ScreenCapture();
var bitmap = sc.CaptureScreenShot();
bitmap.Save(fileName + ".png"), ImageFormat.Png);
于 2013-03-05T08:36:52.283 回答