有没有办法用白色截取整个屏幕的截图?

时间:2013-03-03 23:13:18

标签: winforms white

是否有API可以让我在http://teststack.github.com/White/中执行此操作? 我似乎无法找到它。

由于

的Pawel

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分:1)

通过查看GitHub上的代码,它似乎没有相应的API(也许add it as a feature request?)。

尽管使用Screen类和Graphics.CopyFromScreen的组合,您可以相当简单地完成。有一些示例说明如何在this question的答案中捕获屏幕或活动窗口。

答案 2 :(得分: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);