PhotoConfirmationCapturedEventArgs.Frame 属性

定义

获取捕获的帧。

public:
 property CapturedFrame ^ Frame { CapturedFrame ^ get(); };
CapturedFrame Frame();
public CapturedFrame Frame { get; }
var capturedFrame = photoConfirmationCapturedEventArgs.frame;
Public ReadOnly Property Frame As CapturedFrame

属性值

捕获的帧。

注解

Frame 属性中返回的数据是原始像素数据。 换句话说,它不包括图像文件格式标头。 因此,无法将捕获帧的流直接传递到位图的 SetSourceAsync 方法。 相反,必须将像素数据手动复制到位图的像素缓冲区中。 以下代码片段演示如何复制图像数据并提供用于执行操作的帮助程序类。

首先,需要启用照片确认并连接 PhotoConfirmationCaptured 事件。

private void EnablePhotoConfirmation()
{
    _mediaCapture.VideoDeviceController.PhotoConfirmationControl.Enabled = true;
    _mediaCapture.PhotoConfirmationCaptured += PhotoConfirmationCaptured;
}
void PhotoConfirmationCaptured(MediaCapture sender, PhotoConfirmationCapturedEventArgs args)
{
    using (ManualResetEventSlim evt = new ManualResetEventSlim(false))
    {
        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
        {
            try
            {
                WriteableBitmap bmp = new WriteableBitmap(unchecked((int)args.Frame.Width), unchecked((int)args.Frame.Height));
                using (var istream = args.Frame.AsStream())
                using (var ostream = bmp.PixelBuffer.AsStream())
                {
                    await istream.CopyStreamToAsync(ostream);
                }
            }
            finally
            {
                evt.Set();
            }

        });

        evt.Wait();
    }

}

以下代码片段显示了帮助程序类,该类定义将捕获的帧数据复制到可写位图的像素数据流的扩展方法。 类提供同步和异步方法和重载,可用于指定复制缓冲区大小或使用默认大小。


public static class StreamEx
{
    public static void CopyStreamTo(this Stream inputStream, Stream outputStream)
    {
        inputStream.CopyStreamTo(outputStream, 4096);
    }

    public static void CopyStreamTo(this Stream inputStream, Stream outputStream, int bufferSize)
    {
        if (inputStream == null)
        {
            throw new ArgumentNullException("inputStream");
        }

        if (!inputStream.CanSeek)
        {
            throw new ArgumentException("Cannot seek in the input stream.", "inputStream");
        }

        if (!inputStream.CanRead)
        {
            throw new ArgumentException("Input stream is not readable.", "inputStream");
        }

        if (outputStream == null)
        {
            throw new ArgumentNullException("outputStream");
        }

        if (!outputStream.CanSeek)
        {
            throw new ArgumentException("Cannot seek in the output stream.", "outputStream");
        }

        if (!outputStream.CanWrite)
        {
            throw new ArgumentException("Output stream is not writeable.", "outputStream");
        }

        if (bufferSize <= 0)
        {
            throw new ArgumentOutOfRangeException("bufferSize", "Buffer size is equal to zero or negative.");
        }

        inputStream.Seek(0, SeekOrigin.Begin);
        outputStream.Seek(0, SeekOrigin.Begin);

        byte[] buffer = new byte[bufferSize];
        while (inputStream.Position < inputStream.Length)
        {
            int bytesRead = inputStream.Read(buffer, 0, buffer.Length);
            outputStream.Write(buffer, 0, bytesRead);
        }
    }

    public static Task CopyStreamToAsync(this Stream inputStream, Stream outputStream)
    {
        return Task.Run(() => CopyStreamTo(inputStream, outputStream));
    }

    public static Task CopyStreamToAsync(this Stream inputStream, Stream outputStream, int bufferSize)
    {
        return Task.Run(() => CopyStreamTo(inputStream, outputStream, bufferSize));
    }
}

适用于