已有56人关注
录像,不知道为什么录不了文件
发表在C#图书答疑 2012-08-02
是否精华
版块置顶:
namespace EXT13_08
{
    public partial class Form1 : Form
    {
        cVideo video = null;

        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 打开视频
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            btnPlay.Enabled = false;
            btnStop.Enabled = true;
            btnPz.Enabled = true;
            video = new cVideo(pictureBox1.Handle, pictureBox1.Width, pictureBox1.Height);
            video.StartWebCam();
        }

        /// <summary>
        /// 关闭视频
        /// </summary>
        private void btnStop_Click(object sender, EventArgs e)
        {
            btnPlay.Enabled = true;
            btnStop.Enabled = false;
            btnPz.Enabled = false;
            video.CloseWebcam();
        }



        /// <summary>
        /// 拍照
        /// </summary>
        private void bntPz_Click(object sender, EventArgs e)
        {
            video.GrabImage(pictureBox1.Handle, "d:\\a.bmp");
        }

        /// <summary>
        /// 开始录像
        /// </summary>
        private void btnRecord_Click(object sender, EventArgs e)
        {
            btnStar.Enabled = false;
            btnStop.Enabled = true;
            video.StarKinescope(@"d:\lx.avi");
        }
    }

  public class cVideo     //视频类
    {
        private IntPtr lwndC;       //保存无符号句柄
        private IntPtr mControlPtr; //保存管理指示器
        private int mWidth;
        private int mHeight;

        private const int WM_USER = 0x400;
        private const int WM_CAP_START = WM_USER;
        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_STOP = WM_CAP_START + 68;

        private IntPtr hWndC;



        public cVideo(IntPtr handle, int width, int height)
        {
            mControlPtr = handle; //显示视频控件的句柄
            mWidth = width;      //视频宽度
            mHeight = height;    //视频高度
        }
        /// <summary>
        /// 打开视频设备
        /// </summary>
        public void StartWebCam()
        {
            byte[] lpszName = new byte[100];
            byte[] lpszVer = new byte[100];
            VideoAPI.capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100);
            this.lwndC = VideoAPI.capCreateCaptureWindowA(lpszName, VideoAPI.WS_CHILD | VideoAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);
            if (VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
            {
                VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEWRATE, 100, 0);
                VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SET_PREVIEW, true, 0);
            }
        }

        /// <summary>
        /// 关闭视频设备
        /// </summary>
        public void CloseWebcam()
        {
            VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);
        }


        ///   <summary>   
        ///   拍照
        ///   </summary>   
        ///   <param   name="path">要保存bmp文件的路径</param>   
        public void GrabImage(IntPtr hWndC, string path)
        {
            IntPtr hBmp = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(path);
            VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
        }


        ///   <summary>   
        ///   开始录像
        ///   </summary>   
        ///   <param   name="path">要保存录像的路径</param>   
        public void StarKinescope(string path)
        {
            IntPtr hBmp = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(path);
            VideoAPI.SendMessage(hWndC, WM_CAP_FILE_SET_CAPTURE_FILEA, 0, hBmp.ToInt32());
            VideoAPI.SendMessage(hWndC, WM_CAP_SEQUENCE, 0, 0);
        }


        /// <summary>
        /// 停止录像
        /// </summary>
        public void StopKinescope()
        {
            VideoAPI.SendMessage(hWndC, WM_CAP_STOP, 0, 0);
        }

    }
还需要做其他事情吗?

d盘里没有文件。
分享到:
精彩评论 3
小科_mrkj
学分:43 LV2
2012-08-03
沙发
读者朋友:
    您好,如果录像的话,首先需要调用cVideo类中StartVideo方法,然后再调用StarKinescope方法。
staid
学分:0 LV1
TA的每日心情
开心
2021-01-13 21:56:16
2012-08-03
板凳
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Ex13_09
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        cVideo video;
        private void Form1_Load(object sender, EventArgs e)
        {
            btnStop.Enabled = false;
            video = new cVideo(pictureBox1.Handle, 0, 0, pictureBox1.Width, pictureBox1.Height);
            video.StartVideo();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btnStar.Enabled = false;
            btnStop.Enabled = true;
            video.StarKinescope(@"d:\lx.avi");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            btnStar.Enabled = true;
            btnStop.Enabled = false;
            video.StopKinescope();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

这个是光盘上的源码。窗体启动时,看不到图像,摄像头没有打开。
  
小科_mrkj
学分:43 LV2
2012-08-03
地板
读者朋友:
    您好,请加一下4006751066,我们的企业QQ号码,技术人员会为你回复相关问题。
首页上一页 1 下一页尾页 3 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照