您的位置:首页 > 编程语言 > C#

C#摄像头实现拍照功能的简单代码示例

2013-06-24 08:56 996 查看
C#摄像头实现拍照功能的简单代码示例2009-11-20 来自:网上整理字体大小:【】·摘要:这里将介绍一个C#摄像头实现拍照功能的简单代码示例,代码虽然不短,但是基本上实现了相对应的功能,希望对大家有所帮助。· C#摄像头实现拍照功能的简单代码示例1.using System; 2.using System.Runtime.InteropServices; 3.using System.Drawing; 4.using System.Drawing.Imaging; 5.namespace Video 6.{ 7./// 8./// 一个C#摄像头控制类 9./// 10.publicclass VideoWork 11.{ 12.privateconstint WM_USER = 0x400; 13.privateconstint WS_CHILD = 0x40000000; 14.privateconstint WS_VISIBLE = 0x10000000; 15.privateconstint WM_CAP_START = WM_USER; 16.privateconstint WM_CAP_STOP = WM_CAP_START + 68; 17.privateconstint WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10; 18.privateconstint WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11; 19.privateconstint WM_CAP_SAVEDIB = WM_CAP_START + 25; 20.privateconstint WM_CAP_GRAB_FRAME = WM_CAP_START + 60; 21.privateconstint WM_CAP_SEQUENCE = WM_CAP_START + 62; 22.privateconstint WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20; 23.privateconstint WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63; 24.privateconstint WM_CAP_SET_OVERLAY =WM_CAP_START+ 51; 25.privateconstint WM_CAP_SET_PREVIEW =WM_CAP_START+ 50; 26.privateconstint WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6; 27.privateconstint WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2; 28.privateconstint WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3; 29.privateconstint WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5; 30.privateconstint WM_CAP_SET_SCALE=WM_CAP_START+ 53; 31.privateconstint WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52; 32.private IntPtr hWndC; 33.privatebool bWorkStart = false; 34.private IntPtr mControlPtr; 35.privateint mWidth; 36.privateint mHeight; 37.privateint mLeft; 38.privateint mTop; 39.40./// 41./// 初始化显示图像 42./// 43./// 控件的句柄 44./// 开始显示的左边距 45./// 开始显示的上边距 46./// 要显示的宽度 47./// 要显示的长度 48.public VideoWork(IntPtr handle, int left, int top, int width,int height) 49.{ 50.mControlPtr = handle; 51.mWidth = width; 52.mHeight = height; 53.mLeft = left; 54.mTop = top; 55.} 56.[DllImport("avicap32.dll")] 57.privatestaticextern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID); 58.[DllImport("avicap32.dll")] 59.privatestaticexternint capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize ); 60.[DllImport("User32.dll")] 61.privatestaticexternbool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam); 62./// 63./// 开始显示图像 64./// 65.publicvoid Start() 66.{ 67.if (bWorkStart) 68.return; 69.bWorkStart = true; 70.byte[] lpszName = newbyte[100]; 71.hWndC = capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE ,mLeft,mTop,mWidth,mHeight,mControlPtr,0); 72.if (hWndC.ToInt32() != 0) 73.{ 74.SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0); 75.SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0); 76.SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0); 77.SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0); 78.SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0); 79.SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0); 80.SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0); 81.SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0); 82.//Global.log.Write("SendMessage"); 83.} 84.return; 85.} 86./// 87./// 停止显示 88./// 89.publicvoid Stop() 90.{ 91.SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0); 92.bWorkStart = false; 93.} 94./// 95./// 抓图 96./// 97./// 要保存bmp文件的路径 98.publicvoid GrabImage(string path) 99.{ 100.IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); 101.SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64()); 102.} 103.} 104.} 105.106.//这是一个控制摄像头进行拍照的类,我每次使用GrabImage抓图都是225K的一张照片,我想请问如何才能让我107.//抓到的图片小一些,我想控制在70K左右。不知怎么让拍照的像素变小?108.109.if(this.Request.QueryString["filename"]!=null) 110.{ 111.//获取原图片 112.string filename=this.Request.QueryString["filename"]; 113.Bitmap bmpOld=new Bitmap(this.Server.MapPath("images/" + filename)); 114.//计算缩小比例 115.double d1; 116.if(bmpOld.Height>bmpOld.Width) 117.d1=(double)(MaxLength/(double)bmpOld.Width); 118.else119.d1=(double)(MaxLength/(double)bmpOld.Height); 120.//产生缩图 121.Bitmap bmpThumb=new Bitmap(bmpOld,(int)(bmpOld.Width*d1),(int)(bmpOld.Height*d1)); 122.//清除缓冲 123.Response.Clear(); 124.//生成图片 125.bmpThumb.Save(this.Response.OutputStream,ImageFormat.Jpeg); 126.Response.End(); 127.//释放资源 128.bmpThumb.Dispose(); 129.bmpOld.Dispose(); 130.} C#摄像头实现拍照功能的简单代码示例就介绍到这里。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息