using Windows.Foundation; using Windows.Media.Capture; using Windows.Storage; using Windows.Storage.AccessCache; using Windows.Storage.Streams; using System.Windows.Controls; using Windows.Media; using System.Windows.Media.Imaging; using System; using System.Collections.ObjectModel; using System.Threading.Tasks; using System.Windows; using System.IO; using System.Runtime.InteropServices.WindowsRuntime; namespace Final { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : System.Windows.Window { // MainWindow rootPage = MainWindow.; //private Windows.Foundation.Collections.IPropertySet appSettings; //private const String photoKey = "capturedPhoto"; public MainWindow() { InitializeComponent(); // appSettings = ApplicationData.Current.LocalSettings.Values; // ResetButton.Visibility = Visibility.Collapsed; } private async void CapturePhoto_Click(object sender, System.Windows.RoutedEventArgs e) { var capture = new MediaCapture(); // Find the camera device id to use string deviceId = ""; var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture); for (var i = 0; i < devices.Count; i++) { Console.WriteLine(devices[i]); deviceId = devices[i].Id; } var settings = new MediaCaptureInitializationSettings(); settings.AudioDeviceId = ""; settings.VideoDeviceId = deviceId; try { settings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.Auto; settings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.Video; await capture.InitializeAsync(settings); Windows.Media.MediaProperties.VideoEncodingProperties resolutionMax = null; int max = 0; var resolutions = capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo); for (var i = 0; i < resolutions.Count; i++) { Windows.Media.MediaProperties.VideoEncodingProperties res = (Windows.Media.MediaProperties.VideoEncodingProperties)resolutions[i]; Console.WriteLine("resolution : " + res.Width + "x" + res.Height); if (res.Width * res.Height > max) { max = (int)(res.Width * res.Height); resolutionMax = res; } } await capture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolutionMax); Windows.Media.MediaProperties.ImageEncodingProperties imageProperties = Windows.Media.MediaProperties.ImageEncodingProperties.CreateJpeg(); var fPhotoStream = new InMemoryRandomAccessStream(); await capture.CapturePhotoToStreamAsync(imageProperties, fPhotoStream); await fPhotoStream.FlushAsync(); fPhotoStream.Seek(0); byte[] bytes = new byte[fPhotoStream.Size]; await fPhotoStream.ReadAsync(bytes.AsBuffer(), (uint)fPhotoStream.Size, InputStreamOptions.None); BitmapImage bitmapImage = new BitmapImage(); MemoryStream byteStream = new MemoryStream(bytes); Image image = new System.Windows.Controls.Image(); bitmapImage.BeginInit(); bitmapImage.StreamSource = byteStream; bitmapImage.EndInit(); image.Source = bitmapImage; CapturedPhoto.Source = bitmapImage; msg.Content = resolutionMax.Width; } catch (Exception ex) { msg.Content = ex.Message.ToString(); } } } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)