C# Take window/desktop screenshot using SlimDX

850 views Asked by At

I am new to SlimDX and I've heard that there is a way to capture screenshots using this library. The reason I want to use SlimDX is that I want to capture screenshots much faster than

Graphics.CopyFromScreen()

so that I can make a livestream app running at higher framerates.

I have some code I found on the internet which should capture the desktop, but it always crashes at the line where I create an instance of Device.

I tried changing the DeviceType parameter to Software and the CreateFlags to Multithreaded just to see if anything changes, but nothing did and this is what it says every time:

SlimDX.Direct3D9.Direct3D9Exception: 'D3DERR_INVALIDCALL: Invalid call (-2005530516)'

Here's the code I have:

var pp = new PresentParameters();
pp.Windowed = true;
pp.SwapEffect = SwapEffect.Discard;

var d = new Device(new Direct3D(), 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.SoftwareVertexProcessing, pp);
var surface = Surface.CreateOffscreenPlain(d, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, Format.A8R8G8B8, Pool.Scratch);

d.GetFrontBufferData(0, surface);

//not sure if this will work
var ds = Surface.ToStream(surface, ImageFileFormat.Jpg);
var img = Image.FromStream(ds);

I've also read that it could be a result of BackBuffer not being supported by the graphics card, but in that case I really don't know how to fix this.

My graphics card is AMD R270X.

Any ideas?

1

There are 1 answers

1
Tom Lenc On

Setting pp.BackBufferCount to 0 worked. The capture time is still pretty long though..