当前位置:编程学习 > C#/ASP.NET >>

那句话是定义显示视频的位置


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using LzDirectShow;

namespace Capture
{
    public partial class Form2 : Form
    {
          private bool firstActive;                  // 第一次激活窗口
        private ArrayList capDevices;              // 视频设备列表
        private string fileName;                   // 保存视频avi文件
        private IBaseFilter capFilter;             // 视频设备的 filterinfo
        private IGraphBuilder graphBuilder;        // graphBuilder接口
        private ICaptureGraphBuilder2 capGraph;    // 捕捉Graph接口
        private IVideoWindow videoWin;             // 视频窗口界面
        private IMediaControl mediaCtrl;           // 媒体控制接口
        private IMediaEventEx mediaEvt;            // 媒体事件接口
        private System.Windows.Forms.Button okButton;
        private System.Windows.Forms.Button cancelButton;
        /// <summary> 必需的设计器变量. </summary>
        private System.ComponentModel.Container components = null;
        private const int WM_GRAPHNOTIFY = 0x00008001; // 消息从图
        private const int WS_CHILD = 0x40000000; // 视频窗口的属性
        private const int WS_CLIPCHILDREN = 0x02000000;
        private const int WS_CLIPSIBLINGS = 0x04000000;
        public Form2()
        {
            InitializeComponent();
            ListViewItem item = null;
            //foreach (DsDevice d in devs)
            //{
            //    item = new ListViewItem(d.Name);
            //    item.Tag = d;
            //    deviceListVw.Items.Add(item);
            //}
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            if (capGraph == null)
                return;

            Close();
        }

        private void Form2_Activated(object sender, EventArgs e)
        {
            if (firstActive) return;
            firstActive = true;

            if (!DsUtils.IsCorrectDirectXVersion())
            {
                MessageBox.Show(this, "DirectX 8.1 NOT installed!", "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                this.Close(); return;
            }

            if (!DsDev.GetDevicesOfCat(FilterCategory.VideoInputDevice, out capDevices))
            {
                MessageBox.Show(this, "No video capture devices found!", "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                this.Close(); return;
            }

            SaveFileDialog sd = new SaveFileDialog();
            sd.FileName = @"CaptureNET.avi";
            sd.Title = "Save Video Stream as...";
            sd.Filter = "Video file (*.avi)|*.avi";
            sd.FilterIndex = 1;
            if (sd.ShowDialog() != DialogResult.OK)
            { 
                //this.Close(); return; 
            }
                fileName = sd.FileName;

            DsDevice dev = null;
            if (capDevices.Count == 1)
                dev = capDevices[0] as DsDevice;
            else
            {
                DeviceSelector selector = new DeviceSelector(capDevices);
                selector.ShowDialog(this);                
                dev = selector.SelectedDevice;
            }

            if (dev == null)
            {
                this.Close(); return;
            }

            if (!StartupVideo(dev.Mon))
                this.Close();
            this.Text = "- capturing -";
        }
        
--------------------编程问答--------------------

/// <summary>
        /// Luoz:start all the inte易做图ces, graphs and preview window
        /// </summary>
        bool StartupVideo(IMoniker mon)
        {
            int hr;
            try
            {
                if (!CreateCaptureDevice(mon))
                    return false;

                if (!GetInte易做图ces())
                    return false;

                if (!SetupGraph())
                    return false;

                if (!SetupVideoWindow())
                    return false;

#if DEBUG
                //DsROT.AddGraphToRot(graphBuilder, out rotCookie); // graphBuilder capGraph
#endif

                hr = mediaCtrl.Run();
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(this, "Could not start video stream\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
        }

        /// <summary>
        /// Luoz: 创建用户选择的设备设备
        /// </summary>
        bool CreateCaptureDevice(IMoniker mon)
        {
            object capObj = null;
            try
            {
                Guid gbf = typeof(IBaseFilter).GUID;
                mon.BindToObject(null, null, ref gbf, out capObj);
                capFilter = (IBaseFilter)capObj; capObj = null;
                return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(this, "Could not create capture device\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
            finally
            {
                if (capObj != null)
                    Marshal.ReleaseComObject(capObj); capObj = null;
            }
        }

        /// <summary>
        /// Luoz:创建使用COM组件的接口并获得
        /// </summary>
        /// <returns></returns>
        bool GetInte易做图ces()
        {
            Type comType = null;
            object comObj = null;
            try
            {
                comType = Type.GetTypeFromCLSID(Clsid.FilterGraph);
                if (comType == null)
                    throw new NotImplementedException(@"DirectShow FilterGraph not installed/registered!");
                comObj = Activator.CreateInstance(comType);
                graphBuilder = (IGraphBuilder)comObj; comObj = null;

                //Guid clsid = Clsid.CaptureGraphBuilder2;
                //Guid riid = typeof(ICaptureGraphBuilder2).GUID;
                //comObj = DsBugWO.CreateDsInstance(ref clsid, ref riid);
                //capGraph = (ICaptureGraphBuilder2)comObj; comObj = null;

                comType = Type.GetTypeFromCLSID(Clsid.CaptureGraphBuilder2);
                comObj = Activator.CreateInstance(comType);
                capGraph = (ICaptureGraphBuilder2)comObj; comObj = null;

                mediaCtrl = (IMediaControl)graphBuilder;
                videoWin = (IVideoWindow)graphBuilder;
                mediaEvt = (IMediaEventEx)graphBuilder;
                return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(this, "Could not get inte易做图ces\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
            finally
            {
                if (comObj != null)
                    Marshal.ReleaseComObject(comObj); comObj = null;
            }
        }

        /// <summary>
        /// "Luoz:打造捕捉图"
        /// </summary>
        /// <returns></returns>
        bool SetupGraph()
        {
            int hr;
            IBaseFilter mux = null;
            IFileSinkFilter sink = null;

            try
            {
                hr = capGraph.SetFiltergraph(graphBuilder);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                hr = graphBuilder.AddFilter(capFilter, "Ds.NET Video Capture Device");
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                DsUtils.ShowCapPinDialog(capGraph, capFilter, this.Handle);

                Guid sub = MediaSubType.Avi;
                hr = capGraph.SetOutputFileName(ref sub, fileName, out mux, out sink);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                Guid cat = PinCategory.Capture;
                Guid med = MediaType.Video;
                hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, mux); // 流文件
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                cat = PinCategory.Preview;
                med = MediaType.Video;
                hr = capGraph.RenderStream(ref cat, ref med, capFilter, null, null); // 预览窗口
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(this, "Could not setup graph\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
            finally
            {
                if (mux != null)
                    Marshal.ReleaseComObject(mux); mux = null;
                if (sink != null)
                    Marshal.ReleaseComObject(sink); sink = null;
            }
        }

        /// <summary>
        /// Luoz: 预览
        /// </summary>
        bool SetupVideoWindow()
        {
            int hr;
            try
            {
                // 设置视频窗口是主窗口的子窗口
                hr = videoWin.put_Owner(panel1.Handle);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                // Set video window style
                hr = videoWin.put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                //使用助手功能定位在客户端的视频窗口的所有者窗口矩形
                //ResizeVideoWindow();

                // 使视频窗口可见,现在它已正确定位
                hr = videoWin.put_Visible(DsHlp.OATRUE);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);

                hr = mediaEvt.SetNotifyWindow(this.Handle, WM_GRAPHNOTIFY, IntPtr.Zero);
                if (hr < 0)
                    Marshal.ThrowExceptionForHR(hr);
                return true;
            }
            catch (Exception ee)
            {
                MessageBox.Show(this, "Could not setup video window\r\n" + ee.Message, "DirectShow.NET", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return false;
            }
        }

    }
}
--------------------编程问答-------------------- 知道的谢谢告诉下 --------------------编程问答-------------------- 为什么就没有人回答我的问题啊
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,