我从这里复制了以下代码: https://code.msdn.microsoft.com/windowsapps/WindowsPhone-Store-81-vs-25c80c2a#content
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
imagePreview.Source = bmpImage;
}
当我尝试运行时,未定义capturePreview。我删除了那一行,我得到两个例外:
你知道这个问题吗? Windows phone 8.1 RT app抛出异常:mscorlib.ni.dll中的'System.ArgumentException'
抛出异常:mscorlib.ni.dll中的'System.ArgumentException'
编辑:也可以在此处找到相同的代码:https://msdn.microsoft.com/en-us/library/windows.media.capture.mediacapture.aspx?f=255&MSPPError=-2147217396
答案 0 :(得分:0)
“capturePreview”是捕获元素。您必须在页面中添加以下控件。
<CaptureElement x:Name="capturePreview" Width="320" Height="240" />