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

菜鸟求解,大虾们请指教。————在线等

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Vedio.DAL
{
    public class cVideo
    {
        //导入API函数
        [DllImport("avicap32.dll")]
        private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
        [DllImport("avicap32.dll")]
        private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize);
        [DllImport("User32.dll")]
        private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, int lParam);
        //常量设置
        private const int WM_USER = 0x400;
        private const int WS_CHILD = 0x40000000;
        private const int WS_VISIBLE = 0x10000000;
        private const int WM_CAP_START = WM_USER;
        private const int WM_CAP_STOP = WM_CAP_START + 68;
        private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;
        private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;
        private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
        private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;
        private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;
        private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;
        private const int WM_CAP_SEQUENCE_NOFILE = WM_CAP_START + 63;
        private const int WM_CAP_SET_OVERLAY = WM_CAP_START + 51;
        private const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
        private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START + 6;
        private const int WM_CAP_SET_CALLBACK_ERROR = WM_CAP_START + 2;
        private const int WM_CAP_SET_CALLBACK_STATUSA = WM_CAP_START + 3;
        private const int WM_CAP_SET_CALLBACK_FRAME = WM_CAP_START + 5;
        private const int WM_CAP_SET_SCALE = WM_CAP_START + 53;
        private const int WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52;
        private const int WM_CAP_FILE_SAVEAS = WM_CAP_START + 23;   //将捕捉文件保存为另一个用户指定的文件。这个消息不会改变捕捉文件的名字和内容,

        //全局变量
        private IntPtr hWndC; //句柄
        private IntPtr mControlPtr; //句柄
        private bool bWorkStart = false;
        private int mWidth;  //视频显示宽度
        private int mHeight;   //视频显示高度
        private int mLeft;    //视频显示左边距
        private int mTop;     //视频显示上边距

        ///   <summary>   
        ///   初始化显示图像   
        ///   </summary>   
        ///   <param   name="handle">播放视频控件的句柄</param>   
        ///   <param   name="left">视频显示的左边距</param>   
        ///   <param   name="top">视频显示的上边距</param>   
        ///   <param   name="width">要显示视频的宽度</param>   
        ///   <param   name="height">要显示视频的长度</param>   
        public cVideo(IntPtr handle, int left, int top, int width, int height)
        {
            mControlPtr = handle;
            mWidth = width;
            mHeight = height;
            mLeft = left;
            mTop = top;
        }
        ///   <summary>   
        ///   打开视频 
        ///   </summary>   
        public void StartVideo()
        {
            if (bWorkStart)
                return;
            bWorkStart = true;
            byte[] lpszName = new byte[100];
            //创建播放窗口
            hWndC = capCreateCaptureWindowA(lpszName, WS_CHILD | WS_VISIBLE, mLeft, mTop, mWidth, mHeight, mControlPtr, 0);

            if (hWndC.ToInt32() != 0)
            {
                //显示视频
                SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);
                SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);
                SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);
                SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);
                SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);
                SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 100, 0);
                SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);
                SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);
            }
        }
        ///   <summary>   
        ///   关闭视频  
        ///   </summary>   
        public void StopVideo()
        {
            SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);
            bWorkStart = false;
        }

        ///   <summary>   
        ///   开始录像
        ///   </summary>   
        ///   <param   name="path">要保存录像的路径</param>   
        public void StarKinescope(string path)
        {
            IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
            SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt32());
            SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
        }
        /// <summary>
        /// 停止录像
        /// </summary>
        public void StopKinescope()
        {
            SendMessage(hWndC, WM_CAP_STOP, 0, 0);
        }
        /// <summary>
        /// 拍照
        /// </summary>
        /// <param name="path">文件路径</param>
        public void Images(string path)
        {
            IntPtr imagePath = Marshal.StringToHGlobalAnsi(path);
            SendMessage(hWndC, WM_CAP_SAVEDIB, 0, imagePath.ToInt32());
        }
    }
  
}
请看楼下。。。。 --------------------编程问答-------------------- 以下部分是对以上类中方法的调用
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 Vedio.BLL;
using Vedio.Models;
using Vedio.DAL;


namespace VedioWeb
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        VediocameraManager vm = new VediocameraManager();
        VedionameManager vr = new VedionameManager();
        Label lab = null;
        cVideo video;
        private void Form2_Load(object sender, EventArgs e)
        {
            List<Vediocamera> li = vm.GetVediocameraListByAll();
            for (int i = 0; i < vm.getcount(); i++)
            {
                 lab=new Label();
                 lab.Name = li[i].CameraID.ToString();
                 lab.Text = li[i].CameraName;
                 lab.Location = new Point(40, 40 + (i * 40));
                 //所有的标签都绑定到同一事件
                 lab.Click += new System.EventHandler(btn_click);
                 panel1.Controls.Add(lab);
            }
            video = new cVideo(pictureBox1.Handle, 0, 0, pictureBox1.Width, pictureBox1.Height);
            video.StartVideo();
        }
        /// <summary>
        /// 摄像头列表的单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btn_click(object sender, EventArgs e)
        {
           
        
        
        }
        /// <summary>
        /// pannel加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            axRealAudio1.Visible = false;
            List<Vediocamera> lm=vm.GetVediocameraListByAll();
            tv_info.Nodes.Clear();
            TreeNode tvr = null;
            foreach (Vediocamera item in lm)
            {
                tvr = new TreeNode(item.CameraName);
                List<string> yy = vr.getmany(item.CameraID);
                foreach (string yn in yy)
                {
                    TreeNode tn = new TreeNode(yn);
                    tvr.Nodes.Add(tn);
                }
                tv_info.Nodes.Add(tvr);
            }
           
        }
        /// <summary>
        /// 树形菜单的选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tv_info_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (tv_info.SelectedNode.Level ==0)
            {
                MessageBox.Show("此节点不可选择");
                return;
            }
            else
            {
                axRealAudio1.Visible = true;
                string nm = tv_info.SelectedNode.Text;
                this.axRealAudio1.SetControls("ImageWindow,controlpanel");
                this.axRealAudio1.Source ="C:/Documents and Settings/Administrator/桌面/VedioProgess/VedioWeb/bin/Debug/" + nm + "";
                 //mencoder -oac faac -faacopts br=64:mpeg=2:object=1 -srate 44100 -vf scale=400:240,harddup -ofps 15 -ovc xvid -xvidencopts bitrate=320 待压缩的文件 -o 输出文件名.mp4
                
                this.axRealAudio1.SetLoop(true);
                this.axRealAudio1.SetFullScreen();
                this.axRealAudio1.SetWantErrors(true);
                this.axRealAudio1.AutoStart = true; 
            }
        }
        /// <summary>
        /// 开始录像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_start_Click(object sender, EventArgs e)
        {
            video.StarKinescope(@"d:\" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".3GP");
            btn_cancel.Enabled = true;
            btn_start.Enabled = false;
        }
        /// <summary>
        /// 拍摄照片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_vedio_Click(object sender, EventArgs e)
        {
            video.Images(@"d:\" + DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".JPG");  
        }
        /// <summary>
        /// 结束录制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_cancel_Click(object sender, EventArgs e)
        {
            video.StopKinescope();
            btn_start.Enabled = true;
            btn_cancel.Enabled = false;
        }
        /// <summary>
        /// 退出程序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


这个实例运行没错,视频截图也可以实现,但是视频录制有点问题,视频在刚开始录制的时候会有一点点卡,录制的视频内容似乎只是录制了卡的这一段时间,卡的时间也不相同,停止录制的按钮并没有起到作用,对此我很疑惑,请各位大侠帮我看看到底是什么问题。本人不胜感激 --------------------编程问答-------------------- 还有视频要实现一边录制一边播放的功能,请高手能指点一下,给点实例参考一下。 --------------------编程问答-------------------- 自己都不捡重点,谁会有这个耐心看完这么多代码 --------------------编程问答-------------------- 1楼的是具体的方法,2楼只是1楼 方法的调用,1楼的方法代码实在是没法简化呀。 --------------------编程问答-------------------- 可以把完整代码贴出来,帮你调试下
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,