Expand Minimize

MediaCapture.CapturePhotoToStorageFileAsync | capturePhotoToStorageFileAsync method

Applies to Windows and Windows Phone

Captures a photo to a storage file.

Syntax


mediaCapture.capturePhotoToStorageFileAsync(type, file).done( /* Your success and error handlers */ );

Parameters

type

Type: ImageEncodingProperties

The encoding properties for the output image.

file

Type: IStorageFile

The storage file where the image is saved.

Return value

Type: IAsyncAction

Returns an IAsyncAction object that is used to control the asynchronous operation.

Examples

Here is an example that shows how to preview a webcam using a CaptureElement and how to take a photo and display it in an Image object. The XAML creates a simple UI with a CaptureElement, Image, and some Button objects to interact with the MediaCapture element. In code, there is a method to initialize the MediaCapture object, a method to start previewing the camera attached to the MediaCapture object, a method to stop previewing, and a method to take a photo and display it.


<StackPanel Orientation="Horizontal">
    <CaptureElement x:Name="capturePreview" Width="320" Height="240" />
    <Image x:Name="imagePreivew" 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));

    // imagePreivew is a <Image> object defined in XAML
    imagePreivew.Source = bmpImage;
}


Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Phone Silverlight 8.1 and Windows Runtime apps]

Namespace

Windows.Media.Capture
Windows::Media::Capture [C++]

Metadata

Windows.winmd

Capabilities

webcam
microphone

See also

MediaCapture

 

 

Show:
© 2014 Microsoft