首页 > 解决方案 > 在屏幕捕获期间检测丢帧

问题描述

我正在使用 SharpDx 捕获屏幕(1 到 60fps)。有些帧都是透明的,最终被代码处理和保存。

是否有任何简单/快速的方法来检测这些帧丢失而无需打开生成的位图并查找 alpha 值?

这是我正在使用的(将捕获保存为图像):

try
{
    //Try to get duplicated frame within given time.
    _duplicatedOutput.AcquireNextFrame(MinimumDelay, out var duplicateFrameInformation, out var screenResource);

    //Copy resource into memory that can be accessed by the CPU. 
    using (var screenTexture2D = screenResource.QueryInterface<Texture2D>())
        _device.ImmediateContext.CopySubresourceRegion(screenTexture2D, 0, new ResourceRegion(Left, Top, 0, Left + Width, Top + Height, 1), _screenTexture, 0);

    //Get the desktop capture texture.
    var mapSource = _device.ImmediateContext.MapSubresource(_screenTexture, 0, MapMode.Read, MapFlags.None); //, out var stream);

    #region Get image data

    var bitmap = new System.Drawing.Bitmap(Width, Height, PixelFormat.Format32bppArgb);
    var boundsRect = new System.Drawing.Rectangle(0, 0, Width, Height);

    //Copy pixels from screen capture Texture to GDI bitmap.
    var mapDest = bitmap.LockBits(boundsRect, ImageLockMode.WriteOnly, bitmap.PixelFormat);
    var sourcePtr = mapSource.DataPointer;
    var destPtr = mapDest.Scan0;

    for (var y = 0; y < Height; y++)
    {
        //Copy a single line 
        Utilities.CopyMemory(destPtr, sourcePtr, Width * 4);

        //Advance pointers
        sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch);
        destPtr = IntPtr.Add(destPtr, mapDest.Stride);
    }

    //Release source and dest locks
    bitmap.UnlockBits(mapDest);

    //Bitmap is saved in here!!!

    #endregion

    _device.ImmediateContext.UnmapSubresource(_screenTexture, 0);

    screenResource.Dispose();
    _duplicatedOutput.ReleaseFrame();
}
catch (SharpDXException e)
{
    if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
        throw;
}

这是一个修改后的版本

我也有这个版本(将捕获保存为像素数组):

//Get the desktop capture texture.
var data = _device.ImmediateContext.MapSubresource(_screenTexture, 0, MapMode.Read, MapFlags.None, out var stream);

var bytes = new byte[stream.Length];

//BGRA32 is 4 bytes.
for (var height = 0; height < Height; height++)
{
    stream.Position = height * data.RowPitch;
    Marshal.Copy(new IntPtr(stream.DataPointer.ToInt64() + height * data.RowPitch), bytes, height * Width * 4, Width * 4);
}

我不确定这是否是将屏幕捕获保存为图像和/或像素阵列的最佳方式,但它有点工作。

无论如何,问题是捕获的某些帧是完全透明的,它们对我来说毫无用处。我需要以某种方式完全避免保存它们。

当捕获为像素数组时,我可以简单地检查bytes数组,以了解第 4 项是 255 还是 0。保存为图像时,我可以使用bitmap.GetPixel(0,0).A来了解图像是否有内容。

但是通过这两种方式,我需要完成捕获并获取完整的图像内容,然后才能知道帧是否被丢弃。

有什么方法可以知道帧是否被正确捕获?

标签: c#screen-capturesharpdx

解决方案


您的问题归结为您尝试在计时器上执行此操作。没有办法保证每个滴答/帧的最短执行时间。如果用户选择了一个太高的值,你会得到这样的效果。在最坏的情况下,您可能会在 EventQueue 中排队,直到您遇到异常,因为队列已满。

您需要做的是将速率限制为最大值。如果它的工作速度不如用户想要的那么快,那就是现实。我为这种情况编写了一些简单的速率限制代码:

integer interval = 20;
DateTime dueTime = DateTime.Now.AddMillisconds(interval);

while(true){
  if(DateTime.Now >= dueTime){
    //insert code here

    //Update next dueTime
    dueTime = DateTime.Now.AddMillisconds(interval);
  }
  else{
    //Just yield to not tax out the CPU
    Thread.Sleep(1);
  }
}

只需两个注意事项:

  • 这被设计为在单独的线程中运行。您必须这样运行它,或者使 Thread.Sleep() 适应您选择的多任务处理选项。
  • DateTime.Now 不适合如此小的时间范围(2 位数毫秒)。通常返回值只会每 18 毫秒左右更新一次。它实际上会随着时间而变化。60 FPS 使您大约需要 16 毫秒。您应该使用秒表或类似的东西。

推荐阅读