《C#用摄像头抓取图像演示教学.doc》由会员分享,可在线阅读,更多相关《C#用摄像头抓取图像演示教学.doc(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Good is good, but better carries it.精益求精,善益求善。C#用摄像头抓取图像-C#如何用摄像头抓取图像,如何把图像保存到硬盘收藏usingSystem;usingSystem.Runtime.InteropServices;usingSystem.Drawing;usingSystem.Drawing.Imaging;namespaceWeightSystem/一个控制摄像头的类/publicclassPickprivateconstintWM_USER=0x400;privateconstintWS_CHILD=0x40000000;privatecons
2、tintWS_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_
3、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_CAL
4、LBACK_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_STAR
5、T+52;publicstaticintWM_CAP_DLG_VIDEOSOURCE=(WM_CAP_START+42);/选择摄像头privateIntPtrhWndC;privateboolbStat=false;privateIntPtrmControlPtr;privateintmWidth;privateintmHeight;privateintmLeft;privateintmTop;/初始化摄像头/控件的句柄/开始显示的左边距/开始显示的上边距/要显示的宽度/要显示的长度publicPick(IntPtrhandle,intleft,inttop,intwidth,intheig
6、ht)mControlPtr=handle;mWidth=width;mHeight=height;mLeft=left;mTop=top;DllImport(avicap32.dll)privatestaticexternIntPtrcapCreateCaptureWindowA(bytelpszWindowName,intdwStyle,intx,inty,intnWidth,intnHeight,IntPtrhWndParent,intnID);DllImport(avicap32.dll)privatestaticexternintcapGetVideoFormat(IntPtrhWn
7、d,IntPtrpsVideoFormat,intwSize);DllImport(User32.dll)privatestaticexternboolSendMessage(IntPtrhWnd,intwMsg,intwParam,intlParam);/开始显示图像/publicvoidStart()if(bStat)return;bStat=true;bytelpszName=newbyte100;hWndC=capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE,mLeft,mTop,mWidth,mHeight,mControlPtr
8、,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_PREVI
9、EWRATE,66,0);SendMessage(hWndC,WM_CAP_SET_OVERLAY,1,0);SendMessage(hWndC,WM_CAP_SET_PREVIEW,1,0);return;/停止显示/publicvoidStop()SendMessage(hWndC,WM_CAP_DRIVER_DISCONNECT,0,0);bStat=false;/抓图/要保存bmp文件的路径publicvoidGrabImage(stringpath)IntPtrhBmp=Marshal.StringToHGlobalAnsi(path);SendMessage(hWndC,WM_CA
10、P_SAVEDIB,0,hBmp.ToInt32();/录像/要保存avi文件的路径publicvoidKinescope(stringpath)IntPtrhBmp=Marshal.StringToHGlobalAnsi(path);SendMessage(hWndC,WM_CAP_FILE_SET_CAPTURE_FILEA,0,hBmp.ToInt32();SendMessage(hWndC,WM_CAP_SEQUENCE,0,0);/停止录像/publicvoidStopKinescope()SendMessage(hWndC,WM_CAP_STOP,0,0);/publicvoidS
11、etShow()SendMessage(hWndC,WM_CAP_DLG_VIDEOSOURCE,0,0);/把抓到的图像保存到硬盘privatevoidbutton3_Click(objectsender,EventArgse)trySaveFileDialogSaveFileDialog1=newSaveFileDialog();SaveFileDialog1.Filter=bmpfiles(*.bmp)|*.bmp|jpgfiles(*.jpg)|*.jpg|Allfiles(*.*)|*.*;SaveFileDialog1.FilterIndex=2;/默认图片保存格式JPGSaveFileDialog1.RestoreDirectory=true;SaveFileDialog1.FileName=Pic01;/默认图片保存名称Pic01if(SaveFileDialog1.ShowDialog()=DialogResult.OK)pk.GrabImage(SaveFileDialog1.FileName);catch本文来自CSDN博客,转载请标明出处:-