Ismail Sahin
Ismail Sahin

Reputation: 2710

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?

Upvotes: 4

Views: 1990

Answers (2)

Balasubramani M
Balasubramani M

Reputation: 8338

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.

Upvotes: 6

Andrei Zhukouski
Andrei Zhukouski

Reputation: 3506

Did you try to set some properties to default dialog?

public static async Task CapturePhoto()
        {
            CameraCaptureUI dialog = new CameraCaptureUI();
            Size aspectRatio = new Size(16, 9);  // 1
            dialog.PhotoSettings.CroppedAspectRatio = aspectRatio; // 2
            dialog.PhotoSettings.MaxResolution = CameraCaptureUIMaxPhotoResolution.HighestAvailable; // 3
            dialog.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg; // 4
            dialog.PhotoSettings.AllowCropping = true; //5

            StorageFile file = await dialog.CaptureFileAsync(CameraCaptureUIMode.Photo); // Your new file
        }

In my Windows Store App it works great.

Upvotes: 3

Related Questions