私はDirectShow.NETを使用してWPF用のWebカメラコントロールを作成しています。私は正常にグラフを作成し、私の画面に表示するためにカメラからビデオを得ることができます。ただし、ビデオ出力は、作成中のWPFコントロールとは完全に独立しています。videoWindow.put_owner(hWnd)
を呼び出すことによってビデオウィンドウのオーナーを設定しています。現在のWPFウィンドウのウィンドウハンドルはhWnd
です。 WindowInteropHelper
を使用してそのウィンドウハンドルを取得します。ここでDirectShowビデオウィンドウをWPFコントロールに接続する
は、メインルーチンである:
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。 –
ああ、私はちょうどいくつかのことを試していたし、間違ったコードをコピーしたようだ。私は上記の結果を生成するために使用している実際のコードを反映するように編集した。私はWPFを使用しているので、hWndがゼロ/ヌルである可能性はありますか?それは、私がオンラインで探している唯一の答えです。 –
デバッグして、 'hWnd'が非ゼロであることを確認できますか? 0の場合、動作はかなり期待されます(ただし、あなたが望むものではありません)。おそらく、ハンドルが存在する(割り当てられた)前に、あなたは早すぎてそれを設定しているでしょう。 @RomanR。 –