我也使用 DirectshowLib 和 c# 从网络摄像头录制视频,但在视频重新编码期间,视频预览也会显示,因此我已完成以下步骤
1.我有可用的视频设备
在里面。通过使用 IFilterGrapth2 对象
Graph = (IFilterGraph2)new FilterGraph();
在里面。ICaptureGraphBuilder2 对象通过使用
captureGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
4.filter graphth设置为ICaptureGraphBuilder2对象captureGraph.SetFiltergraph(Graph);
- 然后添加了 soursefilterformoniker() 和 outputfilename()
完整代码如下。它没有任何错误,但它单独打开预览窗口;我想在图片框上显示预览..
如何在图片框中预览窗口?
这是我的完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DirectShowLib;
using DirectShowLib.DMO;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.IO;
namespace Cam_Recording_V1_directshow.net_
{
public partial class Form1 : Form
{
string captureDeviceName = string.Empty;
IFilterGraph2 Graph = null;
IMediaControl m_mediaCtrl = null;
public List<DsDevice> AvailableVideoInputDevices { get; private set; }
IAMVideoProcAmp vpa;
[DllImport("olepro32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
private static extern int OleCreatePropertyFrame(IntPtr hwndOwner, int x, int y,
string lpszCaption, int cObjects,
[In, MarshalAs(UnmanagedType.Interface)] ref object ppUnk,
int cPages, IntPtr pPageClsID, int lcid, int dwReserved, IntPtr pvReserved);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
IBaseFilter capFilter = null;
IBaseFilter asfWriter = null;
IFileSinkFilter pTmpSink = null;
ICaptureGraphBuilder2 captureGraph = null;
object o;
//
//Get list of video devices
//
AvailableVideoInputDevices = new List<DsDevice>();
DsDevice[] videoInputDevices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice);
AvailableVideoInputDevices.AddRange(videoInputDevices);
if (AvailableVideoInputDevices.Count > 0)
{
//
//init capture graph
//
Graph = (IFilterGraph2)new FilterGraph();
captureGraph = (ICaptureGraphBuilder2)new CaptureGraphBuilder2();
//
//sets filter object from graph
//
captureGraph.SetFiltergraph(Graph);
//
//which device will use graph setting
//
Graph.AddSourceFilterForMoniker(AvailableVideoInputDevices.First().Mon, null, AvailableVideoInputDevices.First().Name, out capFilter);
captureDeviceName = AvailableVideoInputDevices.First().Name;
#region WMV
//
//sets output file name,and file type
//
captureGraph.SetOutputFileName(MediaSubType.Asf, /*DateTime.Now.Ticks.ToString() +".wmv"*/ "test.wmv", out asfWriter, out pTmpSink);
//
//configure which video setting is used by graph
//
IConfigAsfWriter lConfig = asfWriter as IConfigAsfWriter;
Guid cat = new Guid("8C45B4C7-4AEB-4f78-A5EC-88420B9DADEF");
lConfig.ConfigureFilterUsingProfileGuid(cat);
#endregion
captureGraph.RenderStream(PinCategory.Preview, MediaType.Video, capFilter, null, null);
captureGraph.RenderStream(PinCategory.Capture, MediaType.Video, capFilter, null, asfWriter);
m_mediaCtrl = Graph as IMediaControl;
m_mediaCtrl.Run();
}
else
{
MessageBox.Show("Video Capture Device Not Found!!");
button1.Visible = false;
}
}