I am developing an Windows Store App in WP8.1. I need to take photo through camera and save it and use it for showing but it throws exception everytime. My code is--
async private void capturePhoto_Tapped(object sender, TappedRoutedEventArgs e)
{
try
{
MediaCapture mediaCapture = new MediaCapture();
StorageFile sf = await ApplicationData.Current.LocalFolder.CreateFileAsync("My Picture", CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties img = new ImageEncodingProperties();
await mediaCapture.CapturePhotoToStorageFileAsync(img, sf);
}
catch
{
}
}
It throws an exception Object must be initailized and if I use InitalizeAsync it shows System.Exception and message is Text related to the Exception could not be found.If I use this code
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desired)
{
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desired);
if (deviceID != null) return deviceID;
else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desired));
}
async private void InitCamera_Click(object sender, RoutedEventArgs e)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
var captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.Photo,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
StorageFile sf = await ApplicationData.Current.LocalFolder.CreateFileAsync("My Picture", CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties img = new ImageEncodingProperties();
await captureManager.CapturePhotoToStorageFileAsync(img, sf);
}
I found the error ------ The requested attribute was not found. (Exception from HRESULT: 0xC00D36E6)-- Can anybody help?
I think there are couple of problems you are facing:
I've tried such code and works just fine:
private bool initialized = false;
private MediaCapture captureManager;
async private void capturePhoto_Tapped(object sender, TappedRoutedEventArgs e)
{
if (initialized) return;
try
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
// StorageFile sf = await ApplicationData.Current.LocalFolder.CreateFileAsync("My Picture", CreationCollisionOption.GenerateUniqueName);
// ImageEncodingProperties img = new ImageEncodingProperties();
// img = ImageEncodingProperties.CreateJpeg();
// await captureManager.CapturePhotoToStorageFileAsync(img, sf);
}
catch (Exception ex) { (new MessageDialog("An error occured")).ShowAsync(); }
}
private void OffButton_Click(object sender, RoutedEventArgs e)
{
captureManager.Dispose();
}
User contributions licensed under CC BY-SA 3.0