2017-03-22 38 views
0

所以我試圖用SlimDX捕獲區域。我的意思是,我不想從[0,0][1920,1080]。 最好我想傳遞一個Rectangle對象來保存捕獲所需的信息。用SlimDX捕獲區域

我想用SlimDX(DirectX)來做到這一點,因爲如果我們看一下像CopyFromScreen這樣的替代品,它將極大地提高捕獲時間。

我需要捕獲大約30塊100x100像素,我認爲使用DirectX可能是我最好的選擇。所有開始的coördinates加上寬度/高度都存儲在整數數組中。

我目前使用下面的代碼:

Rectangle rect = new Rectangle(chunk[0], chunk[1], chunk[2], chunk[3]); 
 
Bitmap temp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb); 
 
Graphics g2 = Graphics.FromImage(temp); 
 
g2.CopyFromScreen(rect.Left, rect.Top, 0, 0, temp.Size, CopyPixelOperation.SourceCopy);

此代碼是生活在一個foreach循環在chunks變量進行迭代。但是,這需要大約500毫秒才能完成30次迭代。

+0

是否有可能重用位圖和圖形?無需在每次迭代時創建並丟棄它們。 –

+0

這是必要的,因爲每次迭代的「座標」都不相同。或者你的意思是別的嗎? – Arjan

+0

我看到你提到它將始終是100x100像素 –

回答

1

as @Nico指出,複製一次然後分割比許多小副本快。這裏是一個例子

 var rects = Enumerable.Range(1, 30) 
      .Select(x => new Rectangle(x, x, x + 100, x + 100)); 

     var bounds = Screen.PrimaryScreen.Bounds; 
     Bitmap bigBmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb); 
     Graphics g2 = Graphics.FromImage(bigBmp); 
     g2.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy); 

     var bmps = rects.Select(rect => 
      { 
       return bigBmp.Clone(rect, PixelFormat.Format32bppArgb); 
      }); 
+0

謝謝你的例子。我還沒有完全使用你的實現,但我做了克隆部分,所以非常感謝。 我從32塊大約500毫秒到32塊塊的平均25.7毫秒! – Arjan