2010-07-23 91 views
1

我想採取全屏幕拍攝,我無法捕捉半透明窗口(如tweetdeck或avast通知)。我看到windows的打印屏幕功能可以。我嘗試使用api調用GetDesktopWindow()或使用來自Screen.PrimaryScreen.Bounds的位圖,但沒有成功。謝謝!打印屏幕如何工作/如何捕捉透明窗口!

+0

可能重複的[C#:如何採取屏幕的一部分的屏幕截圖(http://stackoverflow.com/questions/3306600/c-how-to-屏幕截圖部分) – Incognito 2010-07-23 07:35:35

+0

以及CopyFromScreen()沒有得到半透明的窗口.... – andySF 2010-07-23 07:41:41

回答

2

您需要使用CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt還可以捕獲分層窗口。不幸的是,這不起作用,他們摸索了參數驗證代碼。不愉快的選擇是P/Invoke所需的代碼。下面是確實的樣品形式:

using System; 
using System.Drawing; 
using System.Windows.Forms; 
using System.Runtime.InteropServices; 

namespace WindowsApplication1 { 
    public partial class Form1 : Form { 
    public Form1() { 
     InitializeComponent(); 
    } 
    private void button1_Click(object sender, EventArgs e) { 
     Size sz = Screen.PrimaryScreen.Bounds.Size; 
     IntPtr hDesk = GetDesktopWindow(); 
     IntPtr hSrce = GetWindowDC(hDesk); 
     IntPtr hDest = CreateCompatibleDC(hSrce); 
     IntPtr hBmp = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height); 
     IntPtr hOldBmp = SelectObject(hDest, hBmp); 
     bool b = BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, 0, 0, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt); 
     Bitmap bmp = Bitmap.FromHbitmap(hBmp); 
     SelectObject(hDest, hOldBmp); 
     DeleteObject(hBmp); 
     DeleteDC(hDest); 
     ReleaseDC(hDesk, hSrce); 
     bmp.Save(@"c:\temp\test.png"); 
     bmp.Dispose(); 
    } 

    // P/Invoke declarations 
    [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); 
    } 
} 
+0

非常感謝Hans Passant。你是我的項目真正的幫助:) – andySF 2010-07-23 07:54:50