0

I'm attempting to capture a rendered screen from a Managed DirectX application. Typically, the way to do this is as follows:

Surface renderTarget = device.GetRenderTarget(0);
SurfaceLoader.Save(snapshotName, ImageFileFormat.Bmp, renderTarget);

Which is (in my understanding) shorthand for something like:

Surface renderTarget = device.GetRenderTarget(0);
Surface destTarget = device.CreateOffscreenPlainSurface(ClientRectangle.Width, ClientRectangle.Height, graphicsSettings.WindowedDisplayMode.Format, Pool.SystemMemory);
device.GetRenderTargetData(renderTarget,destTarget);
SurfaceLoader.Save(snapshotName,ImageFileFormat.Bmp, destTarget);

The problem is that on older video cards which don't support non-power-of-two dimension textures, the above fails. I've tried a number of workarounds, but nothing seems to accomplish this seemingly simple task of saving arbitrary-dimensioned screen captures. For example, the following fails on new Bitmap() with an invalid parameter exception (note that this requires creating the device with PresentFlag.LockableBackBuffer):

Surface surf = m_device.GetRenderTarget(0);
GraphicsStream gs = surf.LockRectangle(LockFlags.ReadOnly);
Bitmap bmp = new Bitmap(gs);
bmp.Save(snapshotName, ImageFormat.Png);
surf.UnlockRectangle();

Any tips would be greatly appreciated...I've pretty much exhausted everything I can think of (or turn up on Google)...

Goz
  • 61,365
  • 24
  • 124
  • 204
J23
  • 3,061
  • 6
  • 41
  • 52

1 Answers1

0

Why not create a texture which is the next highest power of 2 and then copy a sub rect? It would get round your issues even if the image saved has a whole load of blank space.

I'm surprised Bitmap has issues, tbh. However .. if thats the case then the above will work even it its not ideal.

Goz
  • 61,365
  • 24
  • 124
  • 204
  • Well...that's what I was getting at with the last attempt (lock the render target bits and create a bitmap from them). I suppose I should've been clearer and said taht in that case, the issue wasn't the texture dimensions - it was the fact that "new Bitmap(gs)" generated an invalid parameter exception with the message that gs doesn't contain any image data... – J23 Nov 07 '10 at 23:52