Reputation: 3589
I'm using the following code to get the color of a particular pixel on the screen but for some reason it is always returning a black color. This is the code I'm using:
Rectangle pixel = new Rectangle((int)screenPosition.X, (int)screenPosition.Y, 1, 1);
Texture2D backBufferData = new Texture2D(ScreenManager.GraphicsDevice, ScreenManager.GraphicsDevice.PresentationParameters.BackBufferWidth, ScreenManager.GraphicsDevice.PresentationParameters.BackBufferHeight);
Color[] waterPixel = new Color[1];
backBufferData.GetData<Color>(0, pixel, waterPixel, 0, 1);
Upvotes: 1
Views: 269
Reputation: 3123
Your problem is that your Texture2D object backBufferData
is NOT actually accessing the back buffer data.
The line where you instantiate backBufferData
does not retrieve the back buffer contents but simply creates a new Texture2D object that is the size of your current viewport.
As of XNA 4.0, there are only two ways to get the color data rendered to screen:
The idiomatic, clean way; Render your scene to a RenderTarget2D object so that you can sample it's contents, then blit that RenderTarget2D object to the back buffer using a SpriteBatch draw operation.
The quick and dirty way; Call ScreenManager.GraphicsDevice.GetBackBufferData to access the back buffer directly. If you need to perform this sampling frequently than this is most likely not your best option as it is dreadfully slow. (and possibly blocks your rendering operations)
Upvotes: 4