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

usb 摄像头编程

小弟 做了一个摄像头监控程序  usb无驱的  调试预览时一直黑屏,抓拍图片保存无图像 ,但录像保存后却正常。。。
请问一下是什么原因哪?多多指教
--------------------编程问答-------------------- 专业打捞沉贴 --------------------编程问答-------------------- 和我的情况一样

 
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace WindowsApplication2
{
    //视频API调用
    public class VideoAPI
    {
        [System.Runtime.InteropServices.DllImport("avicap32.dll")]
        public static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int Width,
            int nHeight, IntPtr hWndParent, int nID);
        [System.Runtime.InteropServices.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_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_PREVIEWRATE = WM_USER + 52;
        public const int WM_CAP_SET_VIDEOFORMAT = WM_USER + 45;
        public const int WM_CAP_START = WM_USER;
        public const int WM_CAP_SAVEDIB = WM_CAP_START + 25;
        public void cla()
        {
        }
    }
    public class cVideo
    {
        private IntPtr lwndC;  //保存无符号句柄
        private IntPtr mControlPtr;  //保存管理指示器
        private int mWidth;
        private int mHeigth;
        public cVideo(IntPtr handle, int width, int heigth)
        {
            mControlPtr = handle;
            mWidth = width;
            mHeigth = heigth;
        }
        /// <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, mHeigth, 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>
         /// <param name="hWndC"></param>
        /// <param name="path">路径</param>
        public void GrabImage(IntPtr hWndC, string path)
        {
            IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);
            VideoAPI.SendMessage(lwndC, VideoAPI.WM_CAP_SAVEDIB, 0, hBmp.ToInt32());
        }
    }
}



--------------------------------分割线(以下为窗体上的代码)-------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            btnOpen.Enabled = false;
            btnCloes.Enabled = true;
            btnToImage.Enabled = true;
            cVideo video = new cVideo(pictureBox1.Handle, pictureBox1.Width, pictureBox1.Height);
            video.StartWebCam();
        }
        private void btnToImage_Click(object sender, EventArgs e)
        {
            cVideo video = new cVideo(pictureBox1.Handle, pictureBox1.Width, pictureBox1.Height);
            video.GrabImage(pictureBox1.Handle,"d:\\a.bmp");
        }
       
    }
}  



但是我换了一台电脑试了一下
 就能显示。
--------------------编程问答-------------------- 我的也是这样。那位知道是怎么回事哦。 --------------------编程问答-------------------- 快点解决啊 --------------------编程问答-------------------- 急用啊 --------------------编程问答-------------------- 帮顶。。。。。。。。因为我也在搞摄像头 不过是5.0的
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,