This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge
public ref class MediaCapture sealed : IClosable
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
class MediaCapture final : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class MediaCapture final : IClosable
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
public sealed class MediaCapture : System.IDisposable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Standard)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class MediaCapture : System.IDisposable
function MediaCapture()
Public NotInheritable Class MediaCapture
Implements IDisposable
Inheritance
Object Platform::Object IInspectable MediaCapture

Examples

The following code sample shows how to create and initialize a MediaCapture object.

// Create and initialze the MediaCapture object.
public async void InitMediaCapture()
    _mediaCapture = null;
    _mediaCapture = new Windows.Media.Capture.MediaCapture();
    // Set the MediaCapture to a variable in App.xaml.cs to handle suspension.
    (App.Current as App).MediaCapture = _mediaCapture;
    await _mediaCapture.InitializeAsync(_captureInitSettings);
    CreateProfile();

For info about how to handle suspension, see Handle app suspend.

<StackPanel Orientation="Horizontal">
    <CaptureElement x:Name="capturePreview" Width="320" Height="240" />
    <Image x:Name="imagePreview" Stretch="None" Width="320" Height="240" />
</StackPanel>
<StackPanel Orientation="Horizontal">
    <Button Click="InitCamera_Click" Content="Initialize Camera" />
    <Button Click="StartCapturePreview_Click" Content="Start Capture Preview" />
    <Button Click="CapturePhoto_Click" Content="Capture Photo"/>
    <Button Click="StopCapturePreview_Click" Content="Stop Capture Preview" />
</StackPanel>
Windows.Media.Capture.MediaCapture captureManager;
async private void InitCamera_Click(object sender, RoutedEventArgs e)
    captureManager = new MediaCapture();
    await captureManager.InitializeAsync();
async private void StartCapturePreview_Click(object sender, RoutedEventArgs e)
    capturePreview.Source = captureManager;
    await captureManager.StartPreviewAsync();
async private void StopCapturePreview_Click(object sender, RoutedEventArgs e)
    await captureManager.StopPreviewAsync();
async private void CapturePhoto_Click(object sender, RoutedEventArgs e)
    ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
    // create storage file in local app storage
    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
        "TestPhoto.jpg",
        CreationCollisionOption.GenerateUniqueName);
    // take photo
    await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file);
    // Get photo as a BitmapImage
    BitmapImage bmpImage = new BitmapImage(new Uri(file.Path));
    // imagePreview is a <Image> object defined in XAML
    imagePreview.Source = bmpImage;
	

Remarks

The MediaCapture class is used to capture audio, video, and images from a camera. For how-to guidance for displaying the camera preview, see Display the camera preview. To quickly get started capturing photos, audio, or video, see Basic photo, video, and audio capture with MediaCapture.

The Camera page is the main hub for how-to guidance for using MediaCapture in your app. In addition to the basic camera tasks, this page links to how-to articles for advanced scenarios including:

  • Using the hardware camera button on devices that have one
  • Handling device and screen orientation
  • Using camera profiles to determine device capabilities
  • Setting the format, resolution, and frame rate of captured video
  • Using AdvancedPhotoCapture to capture HDR or low-light photos
  • Using the VideoDeviceController to access manual camera controls like exposure, white balance, auto-focus, and flash
  • Using effects while capturing video
  • Capturing photo sequences
  • Using MediaFrameReader to get a stream of frames from one or more cameras, including rgb, infrared, and depth cameras
  • Getting a frame from the preview stream
  • The Camera article also links to all of the UWP SDK samples for camera, such as the Camera starter kit sample.

    The InitializeAsync method, which initializes the MediaCapture object, must be called before you can start previewing or capturing from the device. In C# or C++ apps, the first use of the MediaCapture object to call InitializeAsync should be on the STA thread. Calls from an MTA thread may result in undefined behavior. InitializeAsync will launch a consent prompt to get the user's permission for the app to access the microphone or camera. InitializeAsync should be called from the main UI thread of your app. Apps must handle app suspension or termination by properly cleaning up media capture resources. For information on shutting down the MediaCapture object properly, see Basic photo, video, and audio capture with MediaCapture.

    On Windows, music and media capture apps should monitor the SystemMediaTransportControls.SoundLevel to determine whether the audio streams on the app have been Muted. For apps using the MediaCapture object, capture will be automatically stopped when the capture streams of the app are muted. Capture is not re-started automatically when the audio streams are unmuted, so the SoundLevel changed notification can be used to restart capture. Use the SystemMediaTransportControls.PropertyChanged event to determine when the SoundLevel property changes.

    For Windows Phone 8.x apps, music and media apps should clean up the MediaCapture object and associated resources in the Suspending event handler and recreate them in the Resuming event handler.

    In Windows 8.1 audio only apps, if the MediaCategory setting is Other, then high latency mode is used. For low latency, set the MediaCategory setting to Communications.

    Adding an in-place editing Media Foundation Transform effect into the capture preview will have no effect on the stream.

    Windows 8 UWP apps that have declared both the webcam and microphone capabilities will not function in Windows 8.1 if the user has not enabled both the webcam and microphone privacy settings.

    MediaCapture only supports one pass CBR encoding.

    Notes on JPEG: JPEG types are passthrough only. To capture an image, the image encoding profile can be set to Auto or you need to specify an encoding profile that matches the native type. To add an effect, you need to switch to an uncompressed video native media type, such as NV12 or RGB32.

    Notes on H.264: If the native type is H.264, you can record using a video media type with type identical to the native type. You cannot add an effect to an H.264 native type stream. To capture video, the image encoding profile can be set to Auto or you need to specify an encoding profile that matches the native type.

    This class is not agile, which means that you need to consider its threading model and marshaling behavior. For more info, see Threading and Marshaling (C++/CX) and Using Windows Runtime objects in a multithreaded environment (.NET).

    Version history

    Windows version SDK version Value added
  • Basic photo, video, and audio capture with MediaCapture
  • Media capture sample
  • Camera capture UI sample
  • Camera options UI sample
  • Device enumeration sample
  • Real-Time communication sample
  • Media extension sample
  • Supported audio and video formats
  • Real-time communication sample (Windows 10)
  • Camera preview frame sample
  • Video stabilization sample
  • Holographic Mixed Reality Capture sample
  •