2

I am developing Windows 8 WPF desktop app. I need to preview webcam into my app. I come to know that MediaCapture class allows this (http://msdn.microsoft.com/en-US/library/windows/desktop/windows.media.capture.mediacapture) but I am not able to use MediaCapture classs into WPF app any idea, any help how to do this ? Thanks.

Update: I am using following code and it crashes:

 async private void StartWebCamPrev()
    {
        Windows.Media.Capture.MediaCapture mediaCapture = new Windows.Media.Capture.MediaCapture();
        await mediaCapture.InitializeAsync();
        await mediaCapture.StartPreviewAsync();
    }
Software Engineer
  • 131
  • 1
  • 6
  • 12
  • Could you please edit your original question and add in any relevant code, errors/exceptions you're seeing, etc. Thanks! – reuben Jul 05 '12 at 09:49
  • When is `StartWebCamPrev` being called? Also, change `async void` to `async Task` unless `StartWebCamPrev` is an event handler. – Stephen Cleary Jul 05 '12 at 13:19
  • at any time you can call but in my app, it is called in button click. – Software Engineer Jul 05 '12 at 16:07
  • There's a working example here: https://github.com/mmaitre314/MediaCaptureWPF, from [this answer](http://stackoverflow.com/a/31506614/52068). – Mike Jul 20 '15 at 01:07

1 Answers1

-1

The Windows.* namespaces belong to Windows Runtime (WinRT). Most of WinRT UI APIs are not available for desktop/WPF/.NET applications, so your approach won't work. You can use almost any other technique to display webcam preview, since WPF apps can integrate with a lot of other technologies - DirectShow being most notable. I would start with WPF MediaKit.

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • Not quite: Almost 50% of the winrt APIs are available for desktop apps. Unfortunately almost none of the APIs that show UI are available for desktop aps :(. – Larry Osterman Jul 05 '12 at 15:17
  • Maybe - every winrt API is tagged and the documentation for the API shows if it works on the desktop or not. For instance the Windows.Networking.Sockets.StreamSocket class says "Applies to: Metro style apps | desktop apps" – Larry Osterman Jul 05 '12 at 15:31
  • Ahh, yes. Now I can see that. – Filip Skakun Jul 05 '12 at 15:44
  • There is a list of new desktop APIs here that I would assume mostly includes WinRT APIs: http://msdn.microsoft.com/en-us/library/windows/desktop/hh920511(v=vs.85).aspx – Filip Skakun Jul 05 '12 at 15:46
  • I haven't seen however any document on how to use WinRT APIs from say .NET. I found some blog post here: "http://ermau.com/using-winrt-from-net/", but I can't find the winmd files where they used to be. What did I miss? – Filip Skakun Jul 05 '12 at 15:54
  • @Flip Thanks. You mean to say there is no control or way which can take feed from "StartPreviewAsync" ? I have noticed that in Metro app there is "CaptureElement" in XAML which directly takes i/p from "StartPreviewAsync" and works fine. I am looking for similar control or way in WPF. – Software Engineer Jul 05 '12 at 16:10
  • 1
    If you check the documentation for [CaptureElement](http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.captureelement.aspx) - you'll see it says: "**Applies to:** Metro style apps only". You need to use another method in WPF - e.g. the WPF MediaKit. – Filip Skakun Jul 05 '12 at 17:03
  • Thanks Flip. I am thinking to use DirectShow as not want to use any third-party. Do you have any idea on how I can use DirectShow into WPF(C#) ? – Software Engineer Jul 06 '12 at 05:34
  • I've never done it. I would check how it's done in WPF MediaKit. I am almost sure it does not use any patentable algorithms since it has got to be just wrapping some native APIs into WPF controls. You could ask [Jeremiah](http://twitter.com/jmorrill) who created it for help. Otherwise I would start digging into [DirectShow](http://en.wikipedia.org/wiki/DirectShow) or perhaps the newer [Media Foundation](http://en.wikipedia.org/wiki/Media_Foundation) (if you don't care about Windows XP). – Filip Skakun Jul 06 '12 at 09:03
  • @Flip I was just thinking that: If there is no way to use "MediaCapture" into WPF/Desktop app then why MS has provided support for "MediaCapture" for desktop type app. There should be some reason for this. – Software Engineer Jul 06 '12 at 16:49