C#摄像头实现拍照功能的简单代码示例
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("p_w_picpaths/"?+?filename));??
114.//計算縮小比例?
115.double?d1;??
116.if(bmpOld.Height>bmpOld.Width)??
117.d1=(double)(MaxLength/(double)bmpOld.Width);??
118.else
119.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#攝像頭實現拍照功能的簡單代碼示例就介紹到這里。
?
轉載于:https://blog.51cto.com/3446502/1228106
總結
以上是生活随笔為你收集整理的C#摄像头实现拍照功能的简单代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【RK3399Pro学习笔记】十、ROS
- 下一篇: c# char unsigned_dll