2013-05-20 24 views
0

我可以從我的表單(backbufferdata)中獲取「屏幕截圖」,但是是否可以只取其一部分?可以說,如果我的表單是1600x800像素,是否有可能只獲得100x100像素?從表單中獲取特定大小的矩形

int w = GraphicsDevice.PresentationParameters.BackBufferWidth; 
int h = GraphicsDevice.PresentationParameters.BackBufferHeight; 

//pull the picture from the buffer 
int[] backBuffer = new int[w * h]; 
GraphicsDevice.GetBackBufferData(backBuffer); 

//copy into a texture 
Texture2D texture = new Texture2D(GraphicsDevice, w, h, true, GraphicsDevice.PresentationParameters.BackBufferFormat); 
texture.SetData(backBuffer); 

如果我改變寬度和高度,它會給出一個錯誤「它太小或太大」。

+0

嘗試使用,需要一個矩形中的過載。 –

回答

1

嘗試使用GraphicsDevice.GetBackBufferData通用方法(可爲空的,T [],的Int32,Int32)將

作爲例子:

int posX = 0; // area position 
int posY = 0; 
int w = 200; // area size 
int h = 100; 

//pull the picture from the buffer 
int[] backBuffer = new int[w * h]; 
GraphicsDevice.GetBackBufferData(new Rectangle(posX, posY, w, h), backBuffer, 0, w*h); 

//copy into a texture 
Texture2D texture = new Texture2D(GraphicsDevice, w, h, true, GraphicsDevice.PresentationParameters.BackBufferFormat); 
texture.SetData(backBuffer); 
+0

謝謝,這個工程。但是我的「表格」是隱藏的。所以現在我不能從緩衝器中讀出顯然。有沒有辦法解決這個問題? – Did

+0

我不明白。表單是如何隱藏的?如果可能的話,舉例說明。 – Dmi7ry

+0

嗯,它是隱藏的,所以最終用戶看不到表單。 base.WindowState = FormWindowState.Minimized; base.ShowInTaskbar = false; – Did