Esempio n. 1
0
        /// <summary>
        /// Starts the video capture
        /// </summary>
        /// <param name="FrameNumber">the frame number to start at.
        /// Set to 0 to let the control allocate the frame number</param>
        public void Start(ulong frameNumber)
        {
            try
            {
                // for safety, call stop, just in case we are already running
                this.Stop();

                // setup a capture window
                m_captureHandlerWindow = WebCamAPI.capCreateCaptureWindowA("WebCap", 0, 0, 0, ImageWidth, ImageHeight, this.Handle.ToInt32(), 0);

                // connect to the capture device
                Application.DoEvents();
                WebCamAPI.SendMessage(m_captureHandlerWindow, WebCamAPI.WM_CAP_CONNECT, 0, 0);
                WebCamAPI.SendMessage(m_captureHandlerWindow, WebCamAPI.WM_CAP_SET_PREVIEW, 0, 0);

                // set the capture video format (width and height) to the value of the params given at c'tor
                WebCamAPI.BitMapInfo bInfo = new WebCamAPI.BitMapInfo();
                bInfo.bmiHeader            = new WebCamAPI.BitMapInfoHeader();
                bInfo.bmiHeader.biSize     = (uint)Marshal.SizeOf(bInfo.bmiHeader);
                bInfo.bmiHeader.biWidth    = ImageWidth;
                bInfo.bmiHeader.biHeight   = ImageHeight;
                bInfo.bmiHeader.biPlanes   = 1;
                bInfo.bmiHeader.biBitCount = 24;

                IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(bInfo));
                Marshal.StructureToPtr(bInfo, (IntPtr)buffer, true);
                WebCamAPI.SendMessage(m_captureHandlerWindow, WebCamAPI.WM_CAP_SET_VIDEOFORMAT, Marshal.SizeOf(bInfo), (int)buffer);

                // set the frame number
                this.FrameNumber = frameNumber;

                // set the timer information
                //this.m_captureTimer.Interval = this.CaptureInterval;
                m_timer.Period = this.CaptureInterval;
                m_stopped      = false;
                m_timer.Start();
                //this.m_captureTimer.Start();
            }

            catch (Exception excep)
            {
                string errorMsg = "An error ocurred while starting the video capture. Check that your webcamera is connected properly and turned on." +
                                  Environment.NewLine + "Error: " + excep.Message;
                MessageBox.Show(this, errorMsg, "Start Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Stop();
            }
        }