问题描述
想用C#做一个窗体程序,可以打开笔记本的摄像头并显示到picturebox中,但是从网上找到的程序无法正常运行,打开后picturebox直接黑了,笔记本摄像头的指示灯也没有亮,应该怎么解决啊,楼下附代码
解决方案
解决方案二:
publicclassVideoAPI{[DllImport("avicap32.dll")]publicstaticexternIntPtrcapCreateCaptureWindowA(byte[]lpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,IntPtrhWndParent,intnID);[DllImport("avicap32.dll")]publicstaticexternboolcapGetDriverDescriptionA(shortwDriver,byte[]lpszName,intcbName,byte[]lpszVer,intcbVer);[DllImport("User32.dll")]publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,boolwParam,intlParam);[DllImport("User32.dll")]publicstaticexternboolSendMessage(IntPtrhWnd,intwMsg,shortwParam,intlParam);// 常量publicconstintWM_USER=0x400;publicconstintWS_CHILD=0x40000000;publicconstintWS_VISIBLE=0x10000000;publicconstintSWP_NOMOVE=0x2;publicconstintSWP_NOZORDER=0x4;publicconstintWM_CAP_DRIVER_CONNECT=WM_USER+10;publicconstintWM_CAP_DRIVER_DISCONNECT=WM_USER+11;publicconstintWM_CAP_SET_CALLBACK_FRAME=WM_USER+5;publicconstintWM_CAP_SET_PREVIEW=WM_USER+50;publicconstintWM_CAP_SET_PREVIEWRATE=WM_USER+52;publicconstintWM_CAP_SET_VIDEOFORMAT=WM_USER+45;publicconstintWM_CAP_START=WM_USER;publicconstintWM_CAP_SAVEDIB=WM_CAP_START+25;}publicclasscVideo //视频类{privateIntPtrlwndC; //保存无符号句柄privateIntPtrmControlPtr;//保存管理指示器privateintmWidth;privateintmHeight;publiccVideo(IntPtrhandle,intwidth,intheight){mControlPtr=handle;//显示视频控件的句柄mWidth=width; //视频宽度mHeight=height; //视频高度}///<summary>///打开视频设备///</summary>publicvoidStartWebCam(){byte[]lpszName=newbyte[100];byte[]lpszVer=newbyte[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>publicvoidCloseWebcam(){VideoAPI.SendMessage(lwndC,VideoAPI.WM_CAP_DRIVER_DISCONNECT,0,0);}/// <summary> /// 拍照/// </summary> /// <param name="path">要保存bmp文件的路径</param> publicvoidGrabImage(IntPtrhWndC,stringpath){IntPtrhBmp=Marshal.StringToHGlobalAnsi(path);VideoAPI.SendMessage(lwndC,VideoAPI.WM_CAP_SAVEDIB,0,hBmp.ToInt32());}}主窗体添加一个button,响应代码cVideovideo=newcVideo(pictureBox1.Handle,pictureBox1.Width,pictureBox1.Height);video.StartWebCam();
解决方案三:
没有人吗?求大神帮助啊
解决方案四:
笔记本驱动和开关没问题吗,qq视频什么的能打开吗
解决方案五:
引用3楼hanxiao1224的回复:
笔记本驱动和开关没问题吗,qq视频什么的能打开吗
没问题,能正常打开
解决方案六:
因为你用的方法不对。你说的这个方法,对免装驱动的摄像头不管用。一般都算使用directshow处理了,你可以找找C#的类库。或者使用下面的例子:http://download.csdn.net/detail/mjp1234airen4385/3607954
解决方案七:
权限问题
解决方案八:
我尝试了一下你的代码,本来也是黑色后来QQ上我开了一下视频设置,测试窗口变成了PicBox变成了绿的底色有摄像头图片闪烁摄像头是可以调用的,参数应该是有问题的。