internal override Bitmap TakePicture()
    {
        int timeoutMSec = 20000;

        Bitmap result = null;

        // setup & connect
        string captureWTitle = Application.ProductName + " " + m_index;

        m_captureHwnd = NativeMethods.capCreateCaptureWindowA(ref captureWTitle, 0, 0, 0, 320, 240, 0, 0);

        if (NativeMethods.SendMessage(m_captureHwnd, NativeMethods.WM_CAP_DRIVER_CONNECT, m_index, 0) != 0)
        {
            NativeMethods.FrameReceivedEventHandler eventHandler = new NativeMethods.FrameReceivedEventHandler(FrameReceived);

            NativeMethods.SendMessage(m_captureHwnd, NativeMethods.WM_CAP_SET_SCALE, 0, 0);
            NativeMethods.SendMessageCallback(m_captureHwnd, NativeMethods.WM_CAP_SET_CALLBACK_FRAME, 0, eventHandler);

            // get frames until we've saved one
            m_captureTemp = string.Empty;
            int tickStart = Environment.TickCount;
            while ((m_captureTemp.Length == 0) && (Environment.TickCount - tickStart < timeoutMSec))
            {
                NativeMethods.SendMessage(m_captureHwnd, NativeMethods.WM_CAP_GRAB_FRAME, 0, 0);
                if (m_captureTemp.Length == 0)
                {
                    Thread.Sleep(100);
                }
            }

            if (m_captureTemp.Length > 0)
            {
                MemoryStream ms = new MemoryStream(File.ReadAllBytes(m_captureTemp));
                File.Delete(m_captureTemp);
                result = (Bitmap)Bitmap.FromStream(ms);
            }

            NativeMethods.SendMessageCallback(m_captureHwnd, NativeMethods.WM_CAP_SET_CALLBACK_FRAME, 0, null);
            NativeMethods.SendMessage(m_captureHwnd, NativeMethods.WM_CAP_DRIVER_DISCONNECT, m_index, 0);
        }

        // destroy
        NativeMethods.DestroyWindow(m_captureHwnd);

        if (m_captureTemp.Length == 0)
        {
            throw new Exception();
        }

        return(result);
    }
示例#2
0
        internal override Bitmap TakePicture()
        {
            int timeoutMSec = 20000;

            Bitmap result = null;

            // setup & connect
            string captureWTitle = Application.ProductName + " " + m_index;
            m_captureHwnd = NativeMethods.capCreateCaptureWindowA(ref captureWTitle, 0, 0, 0, 320, 240, 0, 0);

            if (NativeMethods.SendMessage(m_captureHwnd, NativeMethods.WM_CAP_DRIVER_CONNECT, m_index, 0) != 0) {
                NativeMethods.FrameReceivedEventHandler eventHandler = new NativeMethods.FrameReceivedEventHandler(FrameReceived);

                NativeMethods.SendMessage(m_captureHwnd, NativeMethods.WM_CAP_SET_SCALE, 0, 0);
                NativeMethods.SendMessageCallback(m_captureHwnd, NativeMethods.WM_CAP_SET_CALLBACK_FRAME, 0, eventHandler);

                // get frames until we've saved one
                m_captureTemp = string.Empty;
                int tickStart = Environment.TickCount;
                while ((m_captureTemp.Length == 0) && (Environment.TickCount - tickStart < timeoutMSec)) {
                    NativeMethods.SendMessage(m_captureHwnd, NativeMethods.WM_CAP_GRAB_FRAME, 0, 0);
                    if (m_captureTemp.Length == 0) {
                        Thread.Sleep(100);
                    }
                }

                if (m_captureTemp.Length > 0) {
                    MemoryStream ms = new MemoryStream(File.ReadAllBytes(m_captureTemp));
                    File.Delete(m_captureTemp);
                    result = (Bitmap)Bitmap.FromStream(ms);
                }

                NativeMethods.SendMessageCallback(m_captureHwnd, NativeMethods.WM_CAP_SET_CALLBACK_FRAME, 0, null);
                NativeMethods.SendMessage(m_captureHwnd, NativeMethods.WM_CAP_DRIVER_DISCONNECT, m_index, 0);
            }

            // destroy
            NativeMethods.DestroyWindow(m_captureHwnd);

            if (m_captureTemp.Length == 0) {
                throw new Exception();
            }

            return result;
        }