代码改变世界

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

2010-08-26 16:04  微软一点都不软  阅读(840)  评论(1编辑  收藏  举报

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. public class VideoWork  
  11. {  
  12. private const int WM_USER = 0x400;  
  13. private const int WS_CHILD = 0x40000000;  
  14. private const int WS_VISIBLE = 0x10000000;  
  15. private const int WM_CAP_START = WM_USER;  
  16. private const int WM_CAP_STOP = WM_CAP_START + 68;  
  17. private const int WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10;  
  18. private const int WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11;  
  19. private const int WM_CAP_SAVEDIB = WM_CAP_START + 25;  
  20. private const int WM_CAP_GRAB_FRAME = WM_CAP_START + 60;  
  21. private const int WM_CAP_SEQUENCE = WM_CAP_START + 62;  
  22. private const int WM_CAP_FILE_SET_CAPTURE_FILEA = WM_CAP_START + 20;  
  23. private const int WM_CAP_SEQUENCE_NOFILE =WM_CAP_START+ 63;  
  24. private const int WM_CAP_SET_OVERLAY =WM_CAP_START+ 51;   
  25. private const int WM_CAP_SET_PREVIEW =WM_CAP_START+ 50;   
  26. private const int WM_CAP_SET_CALLBACK_VIDEOSTREAM = WM_CAP_START +6;  
  27. private const int WM_CAP_SET_CALLBACK_ERROR=WM_CAP_START +2;  
  28. private const int WM_CAP_SET_CALLBACK_STATUSA= WM_CAP_START +3;  
  29. private const int WM_CAP_SET_CALLBACK_FRAME= WM_CAP_START +5;  
  30. private const int WM_CAP_SET_SCALE=WM_CAP_START+ 53;  
  31. private const int WM_CAP_SET_PREVIEWRATE=WM_CAP_START+ 52;   
  32. private IntPtr hWndC;  
  33. private bool bWorkStart = false;  
  34. private IntPtr mControlPtr;  
  35. private int mWidth;  
  36. private int mHeight;  
  37. private int mLeft;  
  38. private int 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. private static extern IntPtr capCreateCaptureWindowA(byte[] lpszWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, int nID);  
  58. [DllImport("avicap32.dll")]  
  59. private static extern int capGetVideoFormat(IntPtr hWnd, IntPtr psVideoFormat, int wSize );  
  60. [DllImport("User32.dll")]   
  61. private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, long lParam);  
  62. ///   
  63. /// 开始显示图像  
  64. ///   
  65. public void Start()  
  66. {  
  67. if (bWorkStart)  
  68. return;  
  69. bWorkStart = true;  
  70. byte[] lpszName = new byte[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. public void Stop()  
  90. {  
  91. SendMessage(hWndC, WM_CAP_DRIVER_DISCONNECT, 0, 0);  
  92. bWorkStart = false;  
  93. }  
  94. ///   
  95. /// 抓图  
  96. ///   
  97. /// 要保存bmp文件的路径  
  98. public void 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的一张照片,我想请问如何才能让我抓到的图片小一些,我想控制在70K左右。不知怎么让拍照的像素变小?  
  107.    
  108. if(this.Request.QueryString["filename"]!=null)  
  109. {  
  110.                 //获取原图片  
  111. string filename=this.Request.QueryString["filename"];  
  112. Bitmap bmpOld=new Bitmap(this.Server.MapPath("images/" + filename));  
  113.     //计算缩小比例  
  114. double d1;  
  115. if(bmpOld.Height>bmpOld.Width)  
  116. d1=(double)(MaxLength/(double)bmpOld.Width);  
  117. else 
  118. d1=(double)(MaxLength/(double)bmpOld.Height);  
  119. //产生缩图  
  120. Bitmap bmpThumb=new Bitmap(bmpOld,(int)(bmpOld.Width*d1),(int)(bmpOld.Height*d1));  
  121. //清除缓冲  
  122. Response.Clear();  
  123. //生成图片  
  124. bmpThumb.Save(this.Response.OutputStream,ImageFormat.Jpeg);  
  125. Response.End();  
  126. //释放资源  
  127. bmpThumb.Dispose();  
  128. bmpOld.Dispose();  

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