2

There are plenty of developers that are frustrated by the fact that the MediaCapture class is available to use in WPF, however, the CaptureElement used to preview the video recording is not.

I understand this limitation, however, I want to use the MediaCapture class to record video.. and I'd like to find a way to provide a preview in WPF if it's possible.

After a lot of digging online, I ran across the CapturedFrame class. It appears that the CapturedFrame is an abstraction that represents a single frame of video recorded from the MediaCapture object.

CapturedFrame MSDN:

https://msdn.microsoft.com/en-us/library/windows/desktop/windows.media.capture.capturedframe.aspx

The CapturedFrame class has a property SoftwareBitmap ..

My question:

Can anyone provide some code or a link which may demonstrate how you could perhaps record a video to a file from MediaCapture.. while processing each frame to some how grab the Bitamp to show the bitmap in WPF with an image control?

I understand that this is not the most effective way to handle the problem, however, if it's possible and it does work, this should be a viable work around for all of us WPF desktop devs that can't use the CaptureElement.

I would like to say that I am aware of WPFMediaKit and other open source projects, however, if this is possible, it's the way that I wish to proceed.

Thanks in advance to any replies.

Bob Billy
  • 35
  • 1
  • 7

1 Answers1

1

There is a sample on the Microsoft github page that is relevant, although they target Windows 10 store apps. If I were to start a new project, I'd go with the Universal Windows Platform instead of WPF, so you may be interested in migrating your project to get this and other functionality.

GetPreviewFrame: This sample will capture preview frames to a SoftwareBitmap and display them in an Image control. Here is the relevant part:

private async Task GetPreviewFrameAsSoftwareBitmapAsync()
{
    // Get information about the preview
    var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

    // Create the video frame to request a SoftwareBitmap preview frame
    var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);

    // Capture the preview frame
    using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
    {
        // Collect the resulting frame
        SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;

        // Copy the SoftwareBitmap to a WriteableBitmap to display it to the user
        var wb = new WriteableBitmap(previewFrame.PixelWidth, previewFrame.PixelHeight);
        previewFrame.CopyToBuffer(wb.PixelBuffer);

        // Display it in the Image control
        PreviewFrameImage.Source = wb;
    }
}

Have a closer look at the sample to see how to get all the details. Or, to have a walkthrough, you can watch the camera session from the recent //build/ conference, which includes a little bit of a walkthrough through some camera samples.

Now, if you do migrate your project, you might as well use the CaptureElement, but I thought you might be interested in seeing this anyway.

Mike
  • 2,220
  • 1
  • 18
  • 32
  • Mike, thanks for the code. I'm trying to test the code and I'm not able to reference access the VideoFrame and SoftwareBitmap types from my WPF app. At this point migrating to a universal app is not possible in my situation (hence my problem..).. Can VideoFrame and SoftwareBitmap be used in WPF (desktop)? If so, are there additional assemblies that I need to reference? Here are the references so far: System.Runtime.dll System.Runtime.InteropServices.WindowsRuntime.dll System.Runtime.WindowsRuntime.dll I can use MediaCapture with no problem. Any ideas? – Bob Billy May 08 '15 at 18:11
  • Yeah, I figured it wouldn't be usable in WPF. But you might want to take a look at how other people are solving this problem: http://stackoverflow.com/questions/26633100/using-winrt-library-in-wpf-application http://stackoverflow.com/questions/14525363/use-mediacapture-on-a-windows-8-desktop-app – Mike May 08 '15 at 21:59
  • When I look at the MSDN documentation for VideoFrame and SoftwareBitmap, the documentation is newer (for Windows 10) and it doesn't have the prereq note that specifies if it's supported on Desktop/Metro or Metro only. Does anyone know if VideoFrame and SoftwareBitamp are Desktop top supported types? – Bob Billy May 18 '15 at 14:41
  • I think they're going to be for modern/store apps only. Sorry! – Mike Jun 15 '15 at 22:36
  • I'd LOVE to see a Windows 8 version of this sample. – Lee Englestone Aug 26 '15 at 10:57
  • I'm afraid that won't be possible, since this API was added in Windows 10. For Windows 8, I recommend you look at this answer: http://stackoverflow.com/questions/27394751/how-to-get-preview-buffer-of-mediacapture-universal-app – Mike Aug 26 '15 at 14:25