During one of my sessions from the Windows 8 Developer Camp I talked about how easy it is for current .NET developers to write Windows 8 Metro style apps. One slide in particular showed how easy it is to interact with devices, etc., through the Windows Runtime in Windows 8 as compared to how difficult it can be when talking to the Win32 API in a traditional desktop application.
Here’s the code from the slide:
using Windows.Media.Capture;
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;}
As you can see, it’s very simple code. And you’ll only need to do a couple things to get it to run properly.
using Windows.UI.Xaml.Media.Imaging; // for BitmapImageusing Windows.Storage; // for FileAccessMode
<Grid Background="{StaticResource ApplicationPageBackgroundBrush}"> <Image x:Name="Photo" Width="700" Height="700" /></Grid>
Once you’ve done that, you’re ready to run your app. And here’s what that looks like.
The first time you run it, the user will be prompted – do you want to allow the program to use your webcam?
Using the webcam to take the picture
Cropping the image (notice it keeps the 4×3 aspect ratio that we specified in the code)
And once you click OK, you’re returned back to the main page of the app
That’s it. Pretty simple, eh?
To keep up with all the stuff that’s going on with Windows 8, one of your best resources is http://dev.windows.com.
-bliz (@snowstormlife)