我使用DirectShow.NET创建WPF Web摄像头的控制。我已经成功创建了一个图表,并可以从相机获取视频以显示在我的屏幕上。但是,视频输出完全独立于其正在创建的WPF控件。附加的DirectShow视频窗口WPF控件
我通过调用videoWindow.put_owner(hWnd)
来设置视频窗口的所有者,其中hWnd
是当前WPF窗口的窗口句柄。我使用WindowInteropHelper
获得该窗口句柄。
这里是主程序:
public void CaptureVideo()
{
int hr = 0;
IBaseFilter sourceFilter = null;
try
{
hr = this.captureGraphBuilder.SetFiltergraph(this.graphBuilder);
DsError.ThrowExceptionForHR(hr);
sourceFilter = FindCaptureDevice();
hr = this.graphBuilder.AddFilter(sourceFilter, "Video Capture");
DsError.ThrowExceptionForHR(hr);
hr = this.captureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, sourceFilter, null, null);
DsError.ThrowExceptionForHR(hr);
Marshal.ReleaseComObject(sourceFilter);
SetupVideoWindow();
hr = this.mediaControl.Run();
DsError.ThrowExceptionForHR(hr);
}
catch
{
Console.WriteLine("An unrecoverable DirectShow error has occurred.");
}
}
而对于SetupVideoWindow()的代码:
public void SetupVideoWindow()
{
int hr = 0;
Window window = Window.GetWindow(this);
var wih = new WindowInteropHelper(window);
IntPtr hWnd = wih.Handle;
hr = this.videoWindow.put_Owner(hWnd);
DsError.ThrowExceptionForHR(hr);
hr = this.videoWindow.put_WindowStyle(DirectShowLib.WindowStyle.Child | DirectShowLib.WindowStyle.ClipChildren);
DsError.ThrowExceptionForHR(hr);
this.videoWindow.SetWindowPosition(0, 0, (int)this.Width, (int)this.Height);
hr = this.videoWindow.put_Visible(OABool.True);
DsError.ThrowExceptionForHR(hr);
}
最后'RenderStream'调用参数是没有意义的。此外,它可能发生,你的'hWnd'是零/空这将解释行为。 –
@RomanR。啊,是的,我只是在尝试一些东西,似乎我复制了错误的代码。我编辑它以反映我用来产生上述结果的实际代码。是否有可能hWnd是零/空,因为我使用的是WPF?这是我似乎在网上找到的唯一答案。 –
那么,你能调试并确保'hWnd'不是零?对于零行为是相当期待的(但不是你想要的)。也许你在把柄存在(分配)之前设置得太早。 –