Hi,
I am building a small webcam API which is part of a larger final year project at my university. In the samples I've seen the camera preview attached to a window, given its HWND handle. The lines of code looked roughly like this (taken from the PlayCap sample in the DirectShow SDK):
...
HWND ghApp = CreateWindow(...);
...
IID_IVideoWindow g_pVW;
...
g_pVW->put_Owner((OAHWND)ghApp);
...
So, what I want to do is actually have the preview go into an Avalon control. I would appreciate any insights on how this may be accomplished.
Thanks in advance,
Cosmin

Webcam preview in an Avalon app
StoneWasHere
Cheers,
Cosmin.
nolla
Here are a few options:
1) Use the HwndHost class. There should be an article in the SDK about the interoperability architecture that covers this. This allows you to easily insert an Hwnd "black box" into a WPF app, but you will not be able to get any kind of advanced compositing (i.e., this window will always be on top with full opacity). Another option with the same limitation is to use an HwndSource.
2) You should be able to implement a custom protocol handler using Windows Media Player. A uri in this form can then be passed on to the MediaElement or MediaPlayer classes. More info on the method that needs to be implemented at http://msdn.microsoft.com/library/default.asp url=/library/en-us/wmform95/htm/wmcreatestreamforurl.asp. The only drawback of this is that the protocol is registered locally for all apps.
3) Set up a streaming server that reads from the webcam, and pass its uri to MediaElement or MediaPlayer.
Thanks,
Ed
RoshanShah
So please tell us that it is coming in the first version of WPF!
Thanks!
Fredrik
Charles Washika
1. is it possible to access any webcam on my system, even if it's a new one for example if I send the app to a friend, which has a different webcam, or if I add a new one, or have multiple ones, and I want to select which one from the URI, etc.
2. would I be able to take pictures with lossless quality at the current resolution while the camera is running in WPF also I want to be able to change resolution on the fly, make adjustments like brightness, saturation, contrast, etc (on the fly).
One more question :) Are you planning to add any support for webcams and the like in WinFX/WPF
Thanks,
Cosmin.
wang ya zheng
This is a good theory but it doesn't work in practicality. I've created a "semi-working" (DirectShow.Net) capture solution which goes something like;
Live WinTV USB2 Capture -> Demux -> MPEG-2 Audio/Video Decoders -> WM ASF Writer (configured with an IWMWriterNetworkSink.)
You can configure WM ASF Writer with a stock WMV7/8/9 profile and this does work in a MediaElement by setting the source to "http://localhost:<your configured port>" -- but you end up with a piddly video resolution, your CPU is taxed to death and there is significant delay.
So then I tried setting a custom profile, sending uncompressed YUY2 samples 720x480 @ 30 fps. The feed works, but only in WMP6 -- WMP10 and MediaElement will not render the uncompressed YUY2 stream. It won't throw an error, it just won't render any video, audio stream renders fine however.
jmorrill on channel9 suggests:
"Create a source filter that reads from your tuner source. Implement the IFileSourceFilter. In filter's register code, make it add a custom protocol in there "mycapturesource://". Code your filter so when it asks to open file "mycapturesource://video1", you open up the tuner on that device. Now in your wpf mediaelement just set the media source to "mycapturesource://video1". Voila!"
I think this might be the only way to go -- if you can figure out how to write your own filter. To a .NET developer this is a pretty daunting task. I assume we would be trying to forward a compressed program stream directly from the capture device to the requester, and let WMP10 figure out which splitter/decoder to use (undesirable, I personally would like to let the user pick their favorite MPEG2 decoder.)
gzou1
Dirk Strikwerda
Has anyone actually managed to use WMCreateStreamForURL to create a stream for MediaElement I can't seem to find any complete examples.
I have been playing with GraphEdit to see if I could get a DirectShow filter to work as suggested on another similar post, but can't work out how to view the graph in windows media player or MediaElement.
There must be an easier and more efficient way of getting live video into WPF than having to write to a series of bitmaps and read them back in.
omegarazor
Unfortunately, we don't have plans on exposing lower level programming options for WPF Media at this time. I'm afraid the options above are the only possibilities.
Thanks for your post,
Ed