我正在使用DirectShow.NET为WPF创建一个Web摄像头控件。我成功地创建了一个图形,并且可以从摄像头获取视频并在屏幕上显示。然而,视频输出与创建它的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);
}
这是正在发生的情况的图片:
![DirectShow.NET视频窗口和WPF MainWindow控件](https://istack.dev59.com/fdVUI.webp)
RenderStream
调用参数没有意义。同时,你的hWnd
可能为零/空,这可以解释这种行为。 - Roman R.hWnd
是非零的吗?对于零,行为是相当预期的(尽管不是你想要的)。也许你在句柄甚至不存在(已分配)之前就设置它了。 - Roman R.