C# 异步方法中的Direct3D11CaptureFramePool帧为空
我正在尝试使用C# 异步方法中的Direct3D11CaptureFramePool帧为空,c#,.net,async-await,C#,.net,Async Await,我正在尝试使用Windows.Graphics.Capture名称空间从可兼容的应用程序中捕获屏幕截图 我的代码基于以下官方示例: 有关守则如下: private async void btnCapture_Click(object sender, EventArgs e) { process = Process.GetProcesses().Where(p => p.ProcessName == "xxxxxxxxxxx").Single(
Windows.Graphics.Capture
名称空间从可兼容的应用程序中捕获屏幕截图
我的代码基于以下官方示例:
有关守则如下:
private async void btnCapture_Click(object sender, EventArgs e)
{
process = Process.GetProcesses().Where(p => p.ProcessName == "xxxxxxxxxxx").Single();
hwnd = process.MainWindowHandle;
GraphicsCaptureItem item = CaptureHelper.CreateItemForWindow(hwnd);
device = Direct3D11Helper.CreateDevice();
capture = new BasicCapture(device, item);
framePool = Direct3D11CaptureFramePool.Create(device, DirectXPixelFormat.B8G8R8A8UIntNormalized, 1, item.Size);
session = framePool.CreateCaptureSession(item);
session.StartCapture();
using (frame = framePool.TryGetNextFrame())
{
var bmp = await SoftwareBitmap.CreateCopyFromSurfaceAsync(frame.Surface);
StorageFolder pictureFolder = KnownFolders.CameraRoll;
StorageFile file = await pictureFolder.CreateFileAsync("test.png", CreationCollisionOption.ReplaceExisting);
using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);
encoder.SetSoftwareBitmap(bmp);
await encoder.FlushAsync();
}
}
}
如果我使用(frame=framePool.TryGetNextFrame())在行中放置一个断点,则会填充该帧,并通过下面的代码正常保存该文件
如果我只是运行应用程序,那么框架为空,并且会发生错误。我无法在framePool.TryGetNextFrame()上使用wait
我觉得这是一个弗兰肯斯坦代码,因为我正在学习新的捕获范式。如何使这段代码工作,有什么帮助吗
最终,我将需要此代码与定时截图一起工作。Direct3D11CaptureFramePool
有一个事件,您可以在尝试获取帧之前侦听该事件:
var cts = new TaskCompletionSource<object>();
framePool.FrameArrived += (s, e) => cts.SetResult(null);
session.StartCapture();
await cts.Task;
// Frame should now be available
using (frame = framePool.TryGetNextFrame())
var cts=new TaskCompletionSource();
framePool.FrameArrived+=(s,e)=>cts.SetResult(null);
session.StartCapture();
等待cts任务;
//框架现在应该可用了
使用(frame=framePool.TryGetNextFrame())