共用方式為


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));
    }
}

適用於