Search code examples
c#imagecamerawindows-store-apps

How to Run windows 8 camera api from c#


I'm trying to use webcam to take picture via c#

previously I used some libraries to do that like;

I faced a problem that when I used default win 8(by the way my operation system is win 8) camera application's maximum photo quality was 8px (3264 x 2468); but using above libraries I programaticaly searched for available snapshot qualities, the maximum size was below 2px. I have no idea how win 8 default camera app makes such a big difference. Therefore I decided to use the default windows 8 camera application.

Problem I googled for solution but not could find any idea about how to programaticaly run win 8 camera app from c#. (Just like mobile phones open camera application: take photo, close cam application, then coppy picture from its location into your application directory) Can anyone help please?


Solution

  • Its very simple. Just follow below steps.

    protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            var ui = new CameraCaptureUI();
            ui.PhotoSettings.CroppedAspectRatio = new Size(4, 3);
            var file = await ui.CaptureFileAsync(CameraCaptureUIMode.Photo);
    
            if (file != null)
            {
                var bitmap = new BitmapImage();
                bitmap.SetSource(await file.OpenAsync(FileAccessMode.Read));
                Photo.Source = bitmap;
            }
        }
    

    Use below header files,

    using Windows.Media.Capture;
    using Windows.UI.Xaml.Media.Imaging; // for BitmapImage
    using Windows.Storage; // for FileAccessMode
    

    Add Photo inside Grid, like this

    <Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
     <Image x:Name="Photo" Width="700" Height="700" />
    </Grid>
    

    And finally, Allow WebCam in Capabilities Which you can find in package.manifest. You are done.