Click here to Skip to main content
15,883,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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();
            }
      } 
        }

        
    }
}
Posted
Comments
AbhinavDAIICT 25-Oct-13 15:25pm    
Looks like there is a problem with this line of code
settings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview;
VideoPreview supports maximum of 640X480 resolution.
This code works fine on PC and captures pictures at greater resolution too but once I test my app on the Tablet, the maximum resolution that I get is 640X480. Please suggest a workaround

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900