从表单中获取特定大小的矩形

时间:2013-05-20 16:39:14

标签: c# xna

我可以从我的表单(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);

如果我更改宽度和高度,则会出现错误“它太小或太大”。

1 个答案:

答案 0 :(得分:1)

尝试使用 GraphicsDevice.GetBackBufferData Generic Method(Nullable,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);