вызывал avicap32 только один раз С#

Я написал программу С#, чтобы сделать снимок с помощью веб-камеры, используя .net framework 2.0. Я пытался использовать avicap32.dll для вызова веб-камеры.

Проблема в том, что я могу просто вызвать свою веб-камеру один раз, если я отключу веб-камеру и снова включу ее с помощью некоторых кнопок, тогда в Picturebox будет отображаться черная фотография и я не смогу снова вызвать веб-камеру. если я снова перезапущу программу, она останется черной в поле с изображением. Только если перезагрузить компьютер. В.Т.Ф. Любой может решить проблему?

Я помещаю исходный код ниже. Пытаюсь понять, что происходит. Еще одна проблема заключается в том, что фото, отображаемое в окне изображения, также не может установить центр изображения.

вот форма1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace AutoCam
{
    public partial class form1 : Form
    {

        public form1()
        {
            InitializeComponent();
        }
        Camera camera;

        #region InitializeComponent

        #region btn_openCamera #endregion
        private void button1_Click(object sender, EventArgs e)
        {

            this.btn_openCamera.Enabled = false;
            this.btn_closeCam.Enabled = true;
            this.btn_cameraPic.Enabled = true;
            camera = new Camera(this.pic_camera.Handle, this.pic_camera.Width, this.pic_camera.Height);
            camera.StartWebCam();
        }
        #endregion

        #region btn_closeCam_Click
        private void btn_closeCam_Click(object sender, EventArgs e)
        {
            this.btn_openCamera.Enabled = true;
            this.btn_closeCam.Enabled = false;
            this.btn_cameraPic.Enabled = false;
            camera.CloseWebcam();
        }
        #endregion

        #region btn_cameraPic_Click
        private void btn_cameraPic_Click(object sender, EventArgs e)
        {
            camera.GrabImage(this.pic_camera.Handle, "guying.bmp");
        }
        #endregion

        private void quit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

вот камера.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows;

namespace AutoCam
{

    class Camera
    {
    private IntPtr lwndC;
        private IntPtr mControlPtr;
        private int mWidth;
        private int mHeight;

        public Camera(IntPtr handel, int width, int height)
        {
            mControlPtr = handel; //handle of video dom
            mWidth = width; //video width
            mHeight = height; //video height
        }

        public void StartWebCam()
        {
            byte[] lpszName = new byte[100];
            byte[] lpszVer = new byte[100];
            CameraAPI.capGetDriverDescriptionA(0, lpszName,100, lpszVer, 0);
            this.lwndC = CameraAPI.capCreateCaptureWindowA(lpszName, CameraAPI.WS_CHILD | CameraAPI.WS_VISIBLE, 0, 0, mWidth, mHeight, mControlPtr, 0);
            if (CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_DRIVER_CONNECT, 0, 0))
            {
                CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SET_PREVIEWRATE, 100, 0);
                CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SET_PREVIEW, true, 0);
            }
        }

        public void CloseWebcam()
        {
            CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_DRIVER_DISCONNECT, 0, 0);
        }

        public void GrabImage(IntPtr hWndC, string path)
        {
            IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
            CameraAPI.SendMessage(lwndC, CameraAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
        }

    }

}

вот cameraAPI.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace AutoCam
{
    class CameraAPI
    {
        [DllImport("avicap32.dll")]
        public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);
        [DllImport("avicap32.dll")]
        public static extern bool capGetDriverDescriptionA(short wDriver, byte[] lpszName, int cbName, byte[] lpszVer, int cbVer);
        [DllImport("User32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, bool wParam, int lParam);
        [DllImport("User32.dll")]
        public static extern bool SendMessage(IntPtr hWnd, int wMsg, short wParam, int lParam);

        public const int WM_USER = 0x400;
        public const int WS_CHILD = 0x40000000;
        public const int WS_VISIBLE = 0x10000000;
        public const int SWP_NOMOVE = 0x2;
        public const int SWP_NOZORDER = 0x4;
        public const int WM_CAP_DRIVER_CONNECT = WM_USER + 10;
        public const int WM_CAP_DRIVER_DISCONNECT = WM_USER +11;
        public const int WM_CAP_SET_CALLBACK_FRAME = WM_USER +5;
        public const int WM_CAP_SET_PREVIEW = WM_USER+50;
        public const int WM_CAP_SET_PREVIEWFORMAT = WM_USER + 45;
        public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
        public const int WM_CAP_START = WM_USER;
        public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;

    }
}

person Bill Tian    schedule 21.08.2013    source источник


Ответы (1)


Вы можете обратиться к этому блогу и изменить свой код. Я попробовал код, который работал хорошо. нажмите стоп:

            bStopped = true;
            this.timer.Stop();

            Application.DoEvents();

            SendMessage(m_CapHwnd, WM_CAP_DISCONNECT, 0, 0);

            CloseClipboard();

нажмите старт:

        stop();

        m_CapHwnd = capCreateCaptureWindowA("WebCap", 0, 0, 0, m_Width, m_Height, this.Handle.ToInt32(), 0);

        Application.DoEvents();

        SendMessage(m_CapHwnd, WM_CAP_CONNECT, 0, 0);

        this.timer.Interval = 1;

        bStopped = false;
        this.timer.Start();

обратный вызов таймера:

        this.timer.Stop();

        SendMessage(m_CapHwnd, WM_CAP_GT_FRAME, 0, 0);

        SendMessage(m_CapHwnd, WM_CAP_COPY, 0, 0);

        OpenClipboard(m_CapHwnd);

        CloseClipboard();

        IDataObject dataObj = Clipboard.GetDataObject();
        Image img = (System.Drawing.Bitmap)dataObj.GetData(DataFormats.Bitmap);
        //img.Save("e:\\test_" + DateTime.UtcNow.Ticks + ".jpg");
        pictureBox.Image = img;

        pictureBox.Refresh();

        Application.DoEvents();

        if (!bStopped)
        {
            this.timer.Start();
        }
person yushulx    schedule 29.08.2013