2013-03-03 66 views

回答

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); 
3

我知道這是一個很老的帖子。但我雖然不能傷害更新它。 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(); 
相關問題