2016-04-25 1 views
0
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
     { 
       try 
       { 
        for (int i = 0; i < 1000; i++) 
        { 
         System.Threading.Thread.Sleep(500); 
         if (pictureBox1.Image != null) 
          pictureBox1.Image.Dispose(); 
         pictureBox1.Image = sc.CaptureWindowToMemory(windowHandle); 
        } 
       } 
       catch(Exception err) 
       { 
        string error = err.ToString(); 
       } 
     } 

そして 画像付きの画像ボックスに数秒かけてパラメータが有効にならないのはなぜですか?

public Bitmap CaptureWindowToMemory(IntPtr handle) 
     { 
      Image img = CaptureWindow(handle); 
      Bitmap bmp = new Bitmap(img); 
      bmp.Save("foo.png", System.Drawing.Imaging.ImageFormat.Png); 
      return bmp; 
     } 

そして
public Image CaptureWindow(IntPtr handle) 
     { 
      // get te hDC of the target window 
      IntPtr hdcSrc = User32.GetWindowDC(handle); 
      // get the size 
      User32.RECT windowRect = new User32.RECT(); 
      User32.GetWindowRect(handle, ref windowRect); 
      int width = windowRect.right - windowRect.left; 
      int height = windowRect.bottom - windowRect.top; 
      // create a device context we can copy to 
      IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc); 
      // create a bitmap we can copy it to, 
      // using GetDeviceCaps to get the width/height 
      IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height); 
      // select the bitmap object 
      IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap); 
      // bitblt over 
      GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY); 
      // restore selection 
      GDI32.SelectObject(hdcDest, hOld); 
      // clean up 
      GDI32.DeleteDC(hdcDest); 
      User32.ReleaseDC(handle, hdcSrc); 
      // get a .NET image object for it 
      Image img = Image.FromHbitmap(hBitmap); 
      // free up the Bitmap object 
      GDI32.DeleteObject(hBitmap); 
      return img; 
     } 

私はそれが動作しますが、画像を表示するには余りにも遅くなり5000msのためにスリープ状態にスレッドを設定した場合

。 50msのような500ミリ秒以下に設定した場合、その後20〜25秒後、私は例外のパラメータを取得していますあなたはそのコントロールにアクセスすると、UIスレッドにディスパッチする必要がある

Application.Run(new Form1()); 

答えて

1

上のProgram.csで有効ではありませんそれによって作成されました。

pictureBox1.Invoke(new Action(() => { 
    if (pictureBox1.Image != null) 
     pictureBox1.Image.Dispose(); 
    pictureBox1.Image = sc.CaptureWindowToMemory(windowHandle); 
})); 

あなたは、単にあなたの背景労働者の代わりにSystem.Windows.Forms.Timerを使用することができように、これはそれがやっているすべてのであればそれは、そうです。

+0

エラーが発生しました:エラーラムダ式をデリゲート型ではないため 'System.Delegate'と入力できません –

+0

文法を修正しました。もう一度やり直してください。 – Blorgbeard

+0

私は1msに設定する前にタイマーを使いましたが、画像の送りは十分に速くありませんでした。また、ここでbackgroundworkerと私はそれをタイマーで少し滑らかに見えた例外の前に500または50msに設定し、まだいくつかの遅れや画像ダーツ/ちらつき目に私はそれが本当に滑らかに動いていないことを意味します。私が画像を取っているプロセスのウィンドウハンドルは、ビデオストリームを表示するプロセスなので、画像を撮って、それをpictureBox1に表示します。他のプロセスではビデオが好きですが、滑らかではありません。他のプロセス。 –

関連する問題