2013-03-03 2 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();