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

开发 后端
这里将介绍一个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.  
  11. public class VideoWork  
  12. {  
  13. private const int WM_USER = 0x400;  
  14. private const int WS_CHILD = 0x40000000;  
  15. private const int WS_VISIBLE = 0x10000000;  
  16. private const int WM_CAP_START = WM_USER;  
  17. private const int WM_CAP_STOP = WM_CAP_START + 68;  
  18. private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;  
  19. private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;  
  20. private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;  
  21. private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;  
  22. private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;  
  23. private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;  
  24. private const int WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;  
  25. private const int WM_CAP_SET_OVERLAY =WM_CAP_START+ 51;   
  26. private const int WM_CAP_SET_PREVIEW =WM_CAP_START+ 50;   
  27. private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;  
  28. private const int WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;  
  29. private const int WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;  
  30. private const int WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;  
  31. private const int WM_CAP_SET_SCALE=WM_CAP_START+ 53;  
  32. private const int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;   
  33. private IntPtr hWndC;  
  34. private bool bWorkStart = false;  
  35. private IntPtr mControlPtr;  
  36. private int mWidth;  
  37. private int mHeight;  
  38. private int mLeft;  
  39. private int mTop;  
  40.  
  41. ///   
  42. /// 初始化显示图像  
  43. /// 
  44.  
  45. /// 控件的句柄  
  46. /// 开始显示的左边距  
  47. /// 开始显示的上边距  
  48. /// 要显示的宽度  
  49. /// 要显示的长度  
  50. public VideoWork(IntPtr handle, int left, int top, int width,int height)  
  51. {  
  52. mControlPtr = handle;  
  53. mWidth = width;  
  54. mHeight = height;  
  55. mLeft = left;  
  56. mTop = top;  
  57. }  
  58. [DllImport("avicap32.dll")]   
  59. private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);  
  60. [DllImport("avicap32.dll")]  
  61. private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize );  
  62. [DllImport("User32.dll")]   
  63. private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);  
  64. ///   
  65. /// 开始显示图像  
  66. /// 
  67.  
  68. public void Start()  
  69. {  
  70. if (bWorkStart)  
  71. return;  
  72. bWorkStart = true;  
  73. byte[] lpszName = new byte[100];  
  74. hWndC = capCreateCaptureWindowA(lpszName,WS_CHILD|WS_VISIBLE ,mLeft,mTop,mWidth,mHeight,mControlPtr,0);  
  75. if (hWndC.ToInt32() != 0)  
  76. {  
  77. SendMessage(hWndC, WM_CAP_SET_CALLBACK_VIDEOSTREAM, 0, 0);  
  78. SendMessage(hWndC, WM_CAP_SET_CALLBACK_ERROR, 0, 0);  
  79. SendMessage(hWndC, WM_CAP_SET_CALLBACK_STATUSA, 0, 0);  
  80. SendMessage(hWndC, WM_CAP_DRIVER_CONNECT, 0, 0);  
  81. SendMessage(hWndC, WM_CAP_SET_SCALE, 1, 0);  
  82. SendMessage(hWndC, WM_CAP_SET_PREVIEWRATE, 66, 0);  
  83. SendMessage(hWndC, WM_CAP_SET_OVERLAY, 1, 0);  
  84. SendMessage(hWndC, WM_CAP_SET_PREVIEW, 1, 0);  
  85. //Global.log.Write("SendMessage");  
  86. }  
  87. return;  
  88. }  
  89. ///   
  90. /// 停止显示  
  91. /// 
  92.  
  93. public void Stop()  
  94. {  
  95. SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);  
  96. bWorkStart = false;  
  97. }  
  98. ///   
  99. /// 抓图  
  100. /// 
  101.  
  102. /// 要保存bmp文件的路径  
  103. public void GrabImage(string path)  
  104. {  
  105. IntPtr hBmp = Marshal.StringToHGlobalAnsi(path);  
  106. SendMessage(hWndC,WM_CAP_SAVEDIB,0,hBmp.ToInt64());  
  107. }  
  108. }  
  109. }  
  110.    
  111. 这是一个控制摄像头进行拍照的类,我每次使用GrabImage抓图都是225K的一张照片,我想请问如何才能让我抓到的图片小一些,我想控制在70K左右。不知怎么让拍照的像素变小?  
  112.    
  113. if(this.Request.QueryString["filename"]!=null)  
  114. {  
  115.                 //获取原图片  
  116. string filename=this.Request.QueryString["filename"];  
  117. Bitmap bmpOld=new Bitmap(this.Server.MapPath("images/" + filename));  
  118.     //计算缩小比例  
  119. double d1;  
  120. if(bmpOld.Height>bmpOld.Width)  
  121. d1=(double)(MaxLength/(double)bmpOld.Width);  
  122. else 
  123. d1=(double)(MaxLength/(double)bmpOld.Height);  
  124. //产生缩图  
  125. Bitmap bmpThumb=new Bitmap(bmpOld,(int)(bmpOld.Width*d1),(int)(bmpOld.Height*d1));  
  126. //清除缓冲  
  127. Response.Clear();  
  128. //生成图片  
  129. bmpThumb.Save(this.Response.OutputStream,ImageFormat.Jpeg);  
  130. Response.End();  
  131. //释放资源  
  132. bmpThumb.Dispose();  
  133. bmpOld.Dispose();  

C#摄像头实现拍照功能的简单代码示例就介绍到这里。

【编辑推荐】

  1. C#服务端与客户端连接实现浅析
  2. C#服务端与客户端连接实现浅谈
  3. C#服务端与客户端通信浅析
  4. C#服务端与客户端通信详解
  5. C#服务端程序实现同步传输字符串浅析
责任编辑:彭凡 来源: 51CTO
相关推荐

2009-08-21 17:55:14

C#获取摄像头

2009-08-21 17:17:49

C#摄像头编程

2009-08-21 17:24:18

C#控制摄像头

2016-02-22 10:30:38

AngularJSHTML5摄像头

2023-02-26 22:06:22

Electron浏览器开发

2022-04-15 11:30:59

代码,Python保存视频

2023-09-14 10:05:33

人工智能智能摄像头

2017-06-20 11:45:52

2021-03-11 10:21:55

特斯拉黑客网络攻击

2013-03-21 09:56:09

2009-09-08 09:31:54

c# CheckBox

2011-04-25 09:16:10

Windows 8

2012-06-23 20:13:44

HTML5

2009-06-17 11:52:01

Linux

2011-09-08 13:53:20

Linux摄像头

2021-07-30 16:22:52

摄像头漏洞CamOver

2009-08-27 18:05:54

C#索引功能

2009-08-06 10:55:46

C#代码解释器

2011-09-13 15:51:11

PhoneGap AP

2018-06-20 11:54:54

点赞
收藏

51CTO技术栈公众号