2016-07-25 33 views
2

PrintWindowちらつき、時には保存される画像は完全に白です。私はいつもPrintWindow</p> ​​ <p>問題は、それがPrintwindow関数を呼び出すときに、この特定のゲーム処理にウィンドウが空白本当に速くなるということであると使用方法を使用してウィンドウをキャプチャしようとしている

だから私は、BitBltを使用してみました:

Bitmap bmp = new Bitmap(rc.right, rc.bottom, System.Drawing.Imaging.PixelFormat.Format24bppRgb); 
Graphics gfxBmp = Graphics.FromImage(bmp); 
IntPtr dest = gfxBmp.GetHdc(); 
IntPtr source = GetWindowDC(hwnd); 

BitBlt(dest, 0, 0, rc.width, rc.height, source, 0, 0, 13369376); 
bmp.Save("test.png"); 

をしかし、保存された画像の上にコードを使用すると、完全に黒です。

PrintWindowがプロセスウィンドウに「ホワイトレイヤー」をフリックさせないようにする方法はありますか?それが可能ならBitBtlは私のためにその問題を解決する必要がありますか?しかし、私のコードで何が間違っていますか?

は、あなたはそれが私のために働いて、このコードを試みることができるあなたの

+0

これはWPF用winformsですか? winformsでは、イメージに描画するだけで、最後には最終的なイメージを画面に再描画するだけで、基本的にグラフィックをバッファリングすることができます。 WPFでは同じことをすることができますが、何をしようとしているかによって異なるアプローチがあります。 BitMapCahcedBrushのように、ハードウェアから直接レンダーすることができます。 – noone392

+1

@ noone392:他のアプリケーションのウィンドウの内容をキャプチャすることについて質問しています。 – IInspectable

+0

Snipping Toolなどの他のツールでスクリーンショットを正常に取得できますか? – andlabs

答えて

1

ありがとうございます。まだ黒い画像が見える場合は、おそらくDWM解決策に進む必要があります。

public Bitmap CaptureWindowImage(IntPtr hWnd) 
    { 
     IntPtr hWndDc = GetDC(hWnd); 
     IntPtr hMemDc = CreateCompatibleDC(hWndDc); 
     IntPtr hBitmap = CreateCompatibleBitmap(hWndDc, window.Rectangle.Width, window.Rectangle.Height); 
     SelectObject(hMemDc, hBitmap); 

     BitBlt(hMemDc, 0, 0, window.Rectangle.Width, window.Rectangle.Height, hWndDc, 0, 0, TernaryRasterOperations.SRCCOPY); 
     Bitmap bitmap = Bitmap.FromHbitmap(hBitmap); 

     DeleteObject(hBitmap); 
     ReleaseDC(window.hWnd, hWndDc); 
     ReleaseDC(IntPtr.Zero, hMemDc); 

     return bitmap; 
    } 

    private enum TernaryRasterOperations : uint 
    { 
     /// <summary>dest = source</summary> 
     SRCCOPY = 0x00CC0020, 
     /// <summary>dest = source OR dest</summary> 
     SRCPAINT = 0x00EE0086, 
     /// <summary>dest = source AND dest</summary> 
     SRCAND = 0x008800C6, 
     /// <summary>dest = source XOR dest</summary> 
     SRCINVERT = 0x00660046, 
     /// <summary>dest = source AND (NOT dest)</summary> 
     SRCERASE = 0x00440328, 
     /// <summary>dest = (NOT source)</summary> 
     NOTSRCCOPY = 0x00330008, 
     /// <summary>dest = (NOT src) AND (NOT dest)</summary> 
     NOTSRCERASE = 0x001100A6, 
     /// <summary>dest = (source AND pattern)</summary> 
     MERGECOPY = 0x00C000CA, 
     /// <summary>dest = (NOT source) OR dest</summary> 
     MERGEPAINT = 0x00BB0226, 
     /// <summary>dest = pattern</summary> 
     PATCOPY = 0x00F00021, 
     /// <summary>dest = DPSnoo</summary> 
     PATPAINT = 0x00FB0A09, 
     /// <summary>dest = pattern XOR dest</summary> 
     PATINVERT = 0x005A0049, 
     /// <summary>dest = (NOT dest)</summary> 
     DSTINVERT = 0x00550009, 
     /// <summary>dest = BLACK</summary> 
     BLACKNESS = 0x00000042, 
     /// <summary>dest = WHITE</summary> 
     WHITENESS = 0x00FF0062 
    } 

    [DllImport("gdi32.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)] 
    static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); 

    [DllImport("gdi32.dll")] 
    private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); 

    [DllImport("gdi32.dll", SetLastError = true)] 
    private static extern IntPtr CreateCompatibleDC(IntPtr hdc); 

    [DllImport("gdi32.dll")] 
    private static extern bool DeleteObject(IntPtr hObject); 

    [DllImport("gdi32.dll")] 
    private static extern IntPtr CreateBitmap(int nWidth, int nHeight, uint cPlanes, uint cBitsPerPel, IntPtr lpvBits); 

    [DllImport("user32.dll")] 
    private static extern IntPtr GetDC(IntPtr hWnd); 

    [DllImport("user32.dll")] 
    private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); 

    [DllImport("gdi32.dll")] 
    private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop); 
関連する問題

 関連する問題