2013-05-26 98 views
2

這是我用來捕獲我的屏幕和鼠標cursour作爲屏幕截圖的一類。 但我想做出某種方式,如果窗體在屏幕中間不捕獲屏幕和窗體背後的區域,但不是它自己的窗體。如何在沒有窗體的情況下捕捉屏幕?

即使窗體在前面,我點擊按鈕或在應用程序運行時更改表單中的某些內容,但不要捕獲它,只需要捕獲屏幕窗體後面的區域,如表單不存在。

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

namespace ScreenShotDemo 
{ 
    public class ScreenCapture 
    { 
     [StructLayout(LayoutKind.Sequential)] 
     struct CURSORINFO 
     { 
      public Int32 cbSize; 
      public Int32 flags; 
      public IntPtr hCursor; 
      public POINTAPI ptScreenPos; 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     struct POINTAPI 
     { 
      public int x; 
      public int y; 
     } 

     [DllImport("user32.dll")] 
     static extern bool GetCursorInfo(out CURSORINFO pci); 

     [DllImport("user32.dll")] 
     static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon); 

     const Int32 CURSOR_SHOWING = 0x00000001; 

     public static Bitmap CaptureScreen(bool CaptureMouse) 
     { 
      Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb); 

      try 
      { 
       using (Graphics g = Graphics.FromImage(result)) 
       { 
        g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy); 

        if (CaptureMouse) 
        { 
         CURSORINFO pci; 
         pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO)); 

         if (GetCursorInfo(out pci)) 
         { 
          if (pci.flags == CURSOR_SHOWING) 
          { 
           DrawIcon(g.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor); 
           g.ReleaseHdc(); 
          } 
         } 
        } 
       } 
      } 
      catch 
      { 
       result = null; 
      } 

      return result; 
     } 
    } 

我的意思是說我會看到,當它運行的形式和我將能夠改變的事情,點擊按鈕,但獲取的截屏,如果我將與我畫圖不會看到表單編輯。

這是在Form1中如何使捕獲:

private void StartRecording_Click(object sender, EventArgs e) 
     { 
      timer1.Enabled = true; 
     } 

和定時器Tick事件:

private void timer1_Tick(object sender, EventArgs e) 
     { 
      using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true)) 
      { 
       ffmp.PushFrame(bitmap); 
      }  
     } 

這條線使實際捕獲:使用(位=(位圖)ScreenCapture.CaptureScreen (true))

回答

8

嗯..隱藏表格?

this.Visible = false;然後運行截圖方法。

像這樣:

protected Bitmap TakeScreenshot(bool cursor) 
{ 
    Bitmap bitmap; 
    this.Visible = false; 
    bitmap = CaptureScreen(cursor); 
    this.Visible = true; 
    return bitmap; 
} 

,並用它在你的代碼,你想要的方式:考慮到你正在使用的後端代碼來控制點擊

private void timer1_Tick(object sender, EventArgs e) 
{ 
    using (bitmap = (Bitmap)ScreenCapture.TakeScreenshot(true)) 
    { 
     ffmp.PushFrame(bitmap); 
    }  
} 
+0

,這是一個更好的解決方案。 –

相關問題