从窗体中获取特定大小的矩形

本文关键字:窗体 获取 | 更新日期: 2023-09-27 18:14:22

我可以采取"屏幕截图"从我的形式(backbufferdata),但它是可能采取只有一部分吗?比方说,如果我的表单是1600x800px,有可能只得到100x100px吗?

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);

如果我改变宽度和高度,它会给出一个错误"它太小或太大"。

从窗体中获取特定大小的矩形

尝试使用GraphicsDevice。GetBackBufferData泛型方法(null, 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);