我正在尝试使用IDXGISwapChain.Present库和Direct3DHook来关联Directx11 EasyHook以获取屏幕截图。我想Direct3DHook是为单屏幕截图编写的,我试图扩展它以获得截屏流。在前几个屏幕截图之后,主机应用程序在随机渲染堆栈中获得异常。
我可以将错误隔离到文件DXHookD3D11.cs中的以下代码:
using (MemoryStream ms = new MemoryStream())
{
// This line crashes the main program, removing this makes the program not crash
res = Texture2D.ToStream(textureDest.Device.ImmediateContext, textureDest, ImageFileFormat.Bmp, ms).Code;
if (res != 0)
{
// this debugmessage never gets printed
this.DebugMessage("PresentHook: ERror in Tostream " + res.ToString());
}
.......
}
答案 0 :(得分:0)
我想我发现了错误。 Direct3DHook使用线程池来运行上面的代码。
ThreadPool.QueueUserWorkItem(delegate
{
using (MemoryStream ms = new MemoryStream())
{
Texture2D.ToStream(textureDest.Device.ImmediateContext, textureDest, ImageFileFormat.Bmp, ms);
SendResponse(ms, requestId);
this.DebugMessage("PresentHook: Send response time: " + (DateTime.Now - startSendResponse).ToString());
}
// Free the textureDest as we no longer need it.
textureDest.Dispose();
textureDest = null;
});
但是Texture.ToStream调用需要是单线程的,因为在内部它可能会发出单线程的GetRenderTarget调用,如果两个调用是异步启动的话会导致异常。我删除了ThreadPool调用,它运行正常。