Search code examples
c#.netwindows-8windows-store-appswebcam

MediaCapture Windows 8 Desktop Application- Can't get it work


I'm trying to implement an image capture via this API, and I need to implement in via desktop application.

The problem is that when I save the image into a file (with CapturePhotoToStorageFileAsync) I get a dark picture (almost black), or I get zero size file.

My code is very simple though:

MediaCapture = new MediaCapture();
await mediaCapture.InitializeAsync();

Windows.Storage.StorageFile photoStorageFile;
String PHOTO_FILE_NAME = "photo.jpg";
photoStorageFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync(PHOTO_FILE_NAME, Windows.Storage.CreationCollisionOption.GenerateUniqueName);
ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg();
await mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photoStorageFile);

I read another thread about it: Captured photos are black when using MediaCapture on Windows Phone 8.1

The problem is that MediaCapture.StartPreviewAsync raised an exception "An unexpected error has occured in the operation requested", and I guess the reason for that is the MediaElement which I don't have (again, desktop).

I'll also glad to find a working example, when I searched, I didn't find any real sample to implement MediaCapture on desktop application, I saw an article but without attached code: http://blogs.msdn.com/b/eternalcoding/archive/2013/10/29/how-to-use-specific-winrt-api-from-desktop-apps-capturing-a-photo-using-your-webcam-into-a-wpf-app.aspx

I also found that others also asked about a sample, but with no real answer...

How can I get a working sample for that?


Solution

  • Getting MediaCapture to preview the camera video in Desktop apps is not trivial. I wasn't sure all the pieces were there so I put together some code on GitHub to try it out and it seems to work ok.

    https://github.com/mmaitre314/MediaCaptureWPF

    One caveat: building the MediaCapture/WPF interop required some native code (C++/CLI), so the app needs to be built as either x86 or x64, but not AnyCPU.