作者:
转自: http://www.cs harp win.com/cs harp space/5956 r 7934.shtml
摘要:这里介绍了C#摄像机实现拍照功能的简单代码示例。 代码虽然不短,但基本上实现了对应的功能。 希望能帮到大家。
C#相机实现拍照功能的简单代码示例
usingSystem; 33558 www.Sina.com/system.runtime.interop services; 33558 www.Sina.com/system.drawing; 33558 www.Sina.com/system.drawing.imaging;usingVideo { //////一个C#照相机控制系统///http://www.Sina.com/3358 www.Sina.com/video work { 3358 http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/ws _ visible=0x 10000000; http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/WM _ cap _ start=WM _ ser; http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/WM _ cap _ stop=WM _ cap _ start 68; 3358 www.Sina.com/http://www.Sina.com/http://www.Sina.com/WM _ cap _ driver _ connect=WM _ cap _ start 10 3333330 WM _ cap _ driver _ disconnect=WM _ cap _ start http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/WM _ cap _ grab _ frame=WM _ cap _ start 60; http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/WM _ cap _ sequence=WM _ cap _ start 62; http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/WM _ cap _ file _ set _ capture _ filea=WM _ com http://www.Sina.com/WM _ cap _ sequence _ nofile=WM _ cap _ start 63 http://www.Sina.com/http://ww.Sina.com/com http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/WM _ cap _ set _ preview=WM _ cap _ start 50; http://www.Sina.com/http://www.Sina.com/http://www.Sina.com/WM _ cap _ set _ callback _ videostream=WM _ cam WM _ cap _ set _ callback _ error=WM _ cap _ ssap http://www.Sina.com/http://www.Sina.com/http://www.com
TATUSA= WM_CAP_START +3; private const int WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5; private const int WM_CAP_SET_SCALE=WM_CAP_START+ 53; private const int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52; private IntPtr hWndC; private bool bWorkStart = false; private IntPtr mControlPtr; private int mWidth; private int mHeight; private int mLeft; private int mTop; /// /// 初始化显示图像 /// /// 控件的句柄 /// 开始显示的左边距 /// 开始显示的上边距 /// 要显示的宽度 /// 要显示的长度 public VideoWork(IntPtr handle, int left, int top, int width,int height) { mControlPtr = handle; mWidth = width; mHeight = height; mLeft = left; mTop = top; } [DllImport("avicap32.dll")] private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID); [DllImport("avicap32.dll")] private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize ); [DllImport("User32.dll")] private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam); /// /// 开始显示图像 /// public void Start() { if (bWorkStart) return; bWorkStart = true; byte[] lpszName = new byte[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); //Global.log.Write("SendMessage"); } return; } /// /// 停止显示 /// public void Stop() { SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0); bWorkStart = false; } /// /// 抓图 /// /// 要保存bmp文件的路径 public void GrabImage(string path) { IntPtr hBmp = Marshal.StringToHGlobalAnsi(path); SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64()); } } } //这是一个控制摄像头进行拍照的类,我每次使用GrabImage抓图都是225K的一张照片,我想请问如何才能让我//抓到的图片小一些,我想控制在70K左右。不知怎么让拍照的像素变小? if(this.Request.QueryString["filename"]!=null) { //获取原图片 string filename=this.Request.QueryString["filename"]; Bitmap bmpOld=new Bitmap(this.Server.MapPath("images/" + filename)); //计算缩小比例 double d1; if(bmpOld.Height>bmpOld.Width) d1=(double)(MaxLength/(double)bmpOld.Width); else d1=(double)(MaxLength/(double)bmpOld.Height); //产生缩图 Bitmap bmpThumb=new Bitmap(bmpOld,(int)(bmpOld.Width*d1),(int)(bmpOld.Height*d1)); //清除缓冲 Response.Clear(); //生成图片 bmpThumb.Save(this.Response.OutputStream,ImageFormat.Jpeg); Response.End(); //释放资源 bmpThumb.Dispose(); bmpOld.Dispose(); }C#摄像头实现拍照功能的简单代码示例就介绍到这里。