Csharp/C#教程:C#实现控制摄像头的类分享

本文实例讲述了C#实现控制摄像头的类。分享给大家供大家参考。具体如下:

///<summary> ///一个控制摄像头的类 ///</summary> publicclassPick { privateconstintWM_USER=0x400; privateconstintWS_CHILD=0x40000000; privateconstintWS_VISIBLE=0x10000000; privateconstintWM_CAP_START=WM_USER; privateconstintWM_CAP_STOP=WM_CAP_START+68; privateconstintWM_CAP_DRIVER_CONNECT=WM_CAP_START+10; privateconstintWM_CAP_DRIVER_DISCONNECT=WM_CAP_START+11; privateconstintWM_CAP_SAVEDIB=WM_CAP_START+25; privateconstintWM_CAP_GRAB_FRAME=WM_CAP_START+60; privateconstintWM_CAP_SEQUENCE=WM_CAP_START+62; privateconstintWM_CAP_FILE_SET_CAPTURE_FILEA=WM_CAP_START+20; privateconstintWM_CAP_SEQUENCE_NOFILE=WM_CAP_START+63; privateconstintWM_CAP_SET_OVERLAY=WM_CAP_START+51; privateconstintWM_CAP_SET_PREVIEW=WM_CAP_START+50; privateconstintWM_CAP_SET_CALLBACK_VIDEOSTREAM=WM_CAP_START+6; privateconstintWM_CAP_SET_CALLBACK_ERROR=WM_CAP_START+2; privateconstintWM_CAP_SET_CALLBACK_STATUSA=WM_CAP_START+3; privateconstintWM_CAP_SET_CALLBACK_FRAME=WM_CAP_START+5; privateconstintWM_CAP_SET_SCALE=WM_CAP_START+53; privateconstintWM_CAP_SET_PREVIEWRATE=WM_CAP_START+52; privateIntPtrhWndC; privateboolbStat=false; privateIntPtrmControlPtr; privateintmWidth; privateintmHeight; privateintmLeft; privateintmTop; ///<summary> ///初始化摄像头 ///</summary> ///<paramname="handle">控件的句柄</param> ///<paramname="left">开始显示的左边距</param> ///<paramname="top">开始显示的上边距</param> ///<paramname="width">要显示的宽度</param> ///<paramname="height">要显示的长度</param> publicPick(IntPtrhandle,intleft,inttop,intwidth,intheight) { mControlPtr=handle; mWidth=width; mHeight=height; mLeft=left; mTop=top; } [DllImport("avicap32.dll")] privatestaticexternIntPtrcapCreateCaptureWindowA(byte[]lpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,IntPtrhWndParent,intnID); [DllImport("avicap32.dll")] privatestaticexternintcapGetVideoFormat(IntPtrhWnd,IntPtrpsVideoFormat,intwSize); [DllImport("User32.dll")] privatestaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,longlParam); ///<summary> ///开始显示图像 ///</summary> publicvoidStart() { if(bStat) return; bStat=true; byte[]lpszName=newbyte[100]; hWndC=capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE,mLeft,mTop,mWidth,mHeight,mControlPtr,0); if(hWndC.ToInt32()!=0) { SendMessage(hWndC,WM_CAP_SET_CALLBACK_VIDEOSTREAM,0,0); SendMessage(hWndC,WM_CAP_SET_CALLBACK_ERROR,0,0); SendMessage(hWndC,WM_CAP_SET_CALLBACK_STATUSA,0,0); SendMessage(hWndC,WM_CAP_DRIVER_CONNECT,0,0); SendMessage(hWndC,WM_CAP_SET_SCALE,1,0); SendMessage(hWndC,WM_CAP_SET_PREVIEWRATE,66,0); SendMessage(hWndC,WM_CAP_SET_OVERLAY,1,0); SendMessage(hWndC,WM_CAP_SET_PREVIEW,1,0); } return; } ///<summary> ///停止显示 ///</summary> publicvoidStop() { SendMessage(hWndC,WM_CAP_DRIVER_DISCONNECT,0,0); bStat=false; } ///<summary> ///抓图 ///</summary> ///<paramname="path">要保存bmp文件的路径</param> publicvoidGrabImage(stringpath) { IntPtrhBmp=Marshal.StringToHGlobalAnsi(path); SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64()); } ///<summary> ///录像 ///</summary> ///<paramname="path">要保存avi文件的路径</param> publicvoidKinescope(stringpath) { IntPtrhBmp=Marshal.StringToHGlobalAnsi(path); SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0,hBmp.ToInt64()); SendMessage(hWndC,WM_CAP_SEQUENCE,0,0); } ///<summary> ///停止录像 ///</summary> publicvoidStopKinescope() { SendMessage(hWndC,WM_CAP_STOP,0,0); } }

希望本文所述对大家的C#程序设计有所帮助。

您可能感兴趣的文章:C#WPF使用AForge类库操作USB摄像头拍照并保存C#使用Aforge调用摄像头拍照的方法C#结合AForge实现摄像头录像C#调用AForge实现摄像头录像的示例代码C#实现IP摄像头的方法C#实现调用本机摄像头实例c#实现摄像头拍照功能示例基于AForge实现C#摄像头视频录制功能

标签: 摄像头 摄像

C++中友元的实例详解

快来领取!你想要的C++/C语言优秀书籍

上述就是C#学习教程:C#实现控制摄像头的类分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/906808.html

(0)
上一篇 2021年10月24日
下一篇 2021年10月24日

精彩推荐