问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;usingSystem.Drawing.Design;namespacehealth{publicclassVideoAPI//视频API类{//视频API调用[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];boolres=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>///<paramname="path">要保存bmp文件的路径</param>publicvoidGrabImage(IntPtrhWndC,stringpath){IntPtrhBmp=Marshal.StringToHGlobalAnsi(path);VideoAPI.SendMessage(lwndC,VideoAPI.WM_CAP_SAVEDIB,0,hBmp.ToInt32());}}}这是调用摄像头代码,但调试时,摄像头一片漆黑
解决方案
解决方案二:
设备管理器中启用摄像头?
解决方案三:
解决方案四:
多次启用直至有图像不过只能暂时解决
解决方案五:
引用3楼qq_34896667的回复:
多次启用直至有图像不过只能暂时解决
我这个调用一次就灰色,无法再调用了
解决方案六:
换第三方组件aforget.net不知道是不是叫这个名字..我有代码可以call我