这是我的第一篇博文,决定以后每个程序都要记录下来,方便以后查阅!

本人小菜一名,本程序也是查阅了网上各位前辈的博客和百度知道所整理出来的一个小程序。

第一次写有点不知道从何写起,先贴一张程序图吧。

程序功能,导入Excel用户模板,模板图如下

通过身份证查找用户,根据准考证好生成图片名。

下面开始进入程序

利用摄像头拍照需要调用win32的avicap32.dll和user32.dll。

avicap32.dll:Windows NT 4.0  avicap32.dll是Windows API应用程序接口相关模块,用于对摄像头和其它视频硬件进行AⅥ电影和视频的截取。

user32.dll:user32.dll是Windows用户界面相关应用程序接口,用于包括Windows处理,基本用户界面等特性,如创建窗口和发送消息。

代码部分

  1. private int hHwnd = ;
  2. private const int port = ;
  3. private DataTable dtExcel;
  4. private string imgName;
  5. public CameraForm()
  6. {
  7. InitializeComponent();
  8. }
  9. public struct videohdr_tag
  10. {
  11. public byte[] lpData;
  12. public int dwBufferLength;
  13. public int dwBytesUsed;
  14. public int dwTimeCaptured;
  15. public int dwUser;
  16. public int dwFlags;
  17. public int[] dwReserved;
  18.  
  19. }
  1. /// <summary>
  2. /// 必需的设计器变量。
  3. /// </summary>
  4.  
  5. [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  6. public static extern int capCreateCaptureWindowA([MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszWindowName, int dwStyle, int x, int y, int nWidth, short nHeight, int hWndParent, int nID);
  7. [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  8. public static extern bool capGetDriverDescriptionA(short wDriver, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszName, int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszVer, int cbVer);
  9. [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  10. public static extern bool DestroyWindow(int hndw);
  11. [DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  12. public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
  13. [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  14. public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
  15. [DllImport("vfw32.dll")]
  16. public static extern string capVideoStreamCallback(int hwnd, videohdr_tag videohdr_tag);
  17. [DllImport("vicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

接下来就要开始要调用摄像头打开

  1. /// <summary>
  2. /// 开启摄像头
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. private void button1_Click(object sender, EventArgs e)
  7. {
  8. if (hHwnd == )
  9. this.OpenCapture();
  10. else
  11. MessageBox.Show("摄像头已经开启!","提示");
  12. }
  1. /// <summary>
  2. /// 打开摄像头
  3. /// </summary>
  4. private void OpenCapture()
  5. {
  6. int intWidth = this.panel1.Width;
  7. int intHeight = this.panel1.Height;
  8. int intDevice = ;
  9. string refDevice = intDevice.ToString();
  10. //创建视频窗口并得到句柄
  11. hHwnd = CameraForm.capCreateCaptureWindowA(ref refDevice, , , , , , this.panel1.Handle.ToInt32(), );
  12. if (CameraForm.SendMessage(hHwnd, 0x40a, intDevice, ) > )
  13. {
  14. CameraForm.SendMessage(this.hHwnd, 0x435, -, );
  15. CameraForm.SendMessage(this.hHwnd, 0x434, 0x42, );
  16. CameraForm.SendMessage(this.hHwnd, 0x432, -, );
  17. CameraForm.SetWindowPos(this.hHwnd, , , , intWidth, intHeight, );
  18. }
  19. else
  20. {
  21. CameraForm.DestroyWindow(this.hHwnd);
  22. }
  23. }

capCreateCaptureWindowA的作用是创建一个视频窗口,摄像头捕捉到的视频图像在此窗口内显示,函数返回值就是代表此窗口的句柄。

接下来关闭摄像头

  1. /// <summary>
  2. ///关闭摄像头
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. private void button2_Click(object sender, EventArgs e)
  7. {
  8. //停止视频注销视频句柄
  9. CameraForm.SendMessage(this.hHwnd, 0x40b, , );
  10. CameraForm.DestroyWindow(this.hHwnd);
  11. hHwnd = ;
  12. }

然后是拍照

  1. /// <summary>
  2. /// 截图
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. private void button3_Click(object sender, EventArgs e)
  7. {
  8. if (hHwnd == )
  9. {
  10. MessageBox.Show("请先开启摄像头!","提示");
  11. return;
  12. }
  13. Preview dlg = new Preview();
  14. try
  15. {
  16. CameraForm.SendMessage(this.hHwnd, 0x41e, , );
  17. IDataObject obj1 = Clipboard.GetDataObject();
  18. if (obj1.GetDataPresent(typeof(Bitmap)))
  19. {
  20. Rectangle rect = new Rectangle(, , , );
  21. Image image1 = (Image)obj1.GetData(typeof(Bitmap));
  22. Bitmap bit = (Bitmap)image1;
  23. //MessageBox.Show( bit.Height.ToString());
  24. //MessageBox.Show(bit.Width.ToString());
  25.  
  26. bit = bit.Clone(new Rectangle(, , , ), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  27. bit = (Bitmap)GetThumbnailImage(bit, , );
  28. dlg.Img = bit;
  29. dlg.ImgName = imgName;
  30. }
  31. }
  32. catch
  33. {
  34. }
  35. dlg.ShowDialog();
  36. }
  1. /// <summary>
  2. /// 将某个图像生成指定尺寸的图像缩放图
  3. /// </summary>
  4. /// <param name="myBitmap">原图像</param>
  5. /// <param name="width">新的图像宽</param>
  6. /// <param name="height">新的图像高</param>
  7. /// <returns></returns>
  8. private Image GetThumbnailImage(Bitmap myBitmap, int width, int height)
  9. {
  10.  
  11. //生成图像的缩放图
  12. Image.GetThumbnailImageAbort myCallback =
  13. new Image.GetThumbnailImageAbort(ThumbnailCallback);
  14.  
  15. Image myThumbnail = myBitmap.GetThumbnailImage(
  16. width, height, myCallback, IntPtr.Zero);
  17. //this.pictureBox1.Image = myThumbnail;
  18.  
  19. return myThumbnail;
  20. }
  21. public bool ThumbnailCallback()
  22. {
  23. return false;
  24. }

最后是保存截图

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
  4. SaveFileDialog1.FileName = imgName;
  5. SaveFileDialog1.Filter = "Image Files(*.JPG;*.GIF)|*.JPG;*.GIF|All files (*.*)|*.*";
  6. if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
  7. {
  8. img.Save(SaveFileDialog1.FileName, ImageFormat.Bmp);
  9. this.Close();
  10. }
  11. }

下面是操作Excel

首先判断Excel版本

  1. /// <summary>
  2. /// 检测Excel版本信息
  3. /// </summary>
  4. /// <returns></returns>
  5. public static double JongCheckExcelVer()
  6. {
  7. Type objExcelType = Type.GetTypeFromProgID("Excel.Application");
  8. if (objExcelType == null)
  9. {
  10. return ;
  11. }
  12. object objApp = Activator.CreateInstance(objExcelType);
  13. if (objApp == null)
  14. {
  15. return ;
  16. }
  17. object objVer = objApp.GetType().InvokeMember("Version", BindingFlags.GetProperty, null, objApp, null);
  18. double iVer = Convert.ToDouble(objVer.ToString());
  19. objVer = null;
  20. objApp = null;
  21. objExcelType = null;
  22. GC.Collect();
  23. return iVer;
  24. }
  25.  
  26. public static String JongGetExcelVerStr()
  27. {
  28. String s1;
  29. double excelver;
  30. excelver = JongCheckExcelVer();// ExistsExcelRegedit();
  31. s1 = " Office ";
  32. if (excelver == )
  33. {
  34. MessageBox.Show("無法識別Excel的版本", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
  35. s1 = "無法識別 office 版本";
  36. }
  37. else if (excelver >= ) s1 += "2010或以上";
  38. else if (excelver >= ) s1 += "";
  39. else if (excelver >= ) s1 += "";
  40. else if (excelver >= ) s1 += "XP";
  41. else if (excelver >= ) s1 += "";
  42. else if (excelver >= ) s1 += "";
  43. else if (excelver >= ) s1 += "";
  44.  
  45. return s1;
  46. }

接着是导入到DataTable

  1. /// <summary>
  2. /// 导入excel到datata
  3. /// </summary>
  4. /// <param name="sender"></param>
  5. /// <param name="e"></param>
  6. private void button5_Click(object sender, EventArgs e)
  7. {
  8. System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
  9. try
  10. {
  11. if (fd.ShowDialog() == DialogResult.OK)
  12. {
  13. DataSet ds = new DataSet();
  14.  
  15. string strConn = "";
  16.  
  17. if (JongCheckExcelVer() >= )
  18. {
  19. strConn = "Provider=Microsoft.Ace.OLEDB.12.0;" + "Data Source=" + fd.FileName + ";" + "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
  20. }
  21. else if (JongCheckExcelVer() >= )
  22. {
  23. strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fd.FileName + ";" + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
  24. }
  25. else if (JongCheckExcelVer() == )
  26. {
  27. MessageBox.Show("无法识别Excel的版本", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
  28. }
  29.  
  30. OleDbConnection conn = new OleDbConnection(strConn);
  31. conn.Open();
  32. string strExcel = "";
  33. //string strSheet = "";
  34. string strSheetName = "";
  35. OleDbDataAdapter myCommand = null;
  36. DataTable dtExcelName = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
  37.  
  38. strSheetName = dtExcelName.Rows[][].ToString();
  39.  
  40. strExcel = string.Format("select * from [{0}$]", "Sheet1"); //Bicycle为excel中工作薄
  41. myCommand = new OleDbDataAdapter(strExcel, strConn);
  42. myCommand.Fill(ds, "Sheet1");
  43. System.Data.DataTable dt = new System.Data.DataTable();
  44. dt = ds.Tables[];
  45. dtExcel = dt;
  46. MessageBox.Show("导入成功!","提示");
  47. }
  48. else
  49. MessageBox.Show("导入失败!","提示");
  50. }
  51. catch
  52. {
  53. MessageBox.Show("请选择相应的excel文件", "警告");
  54. }
  55. }

本程序代码大部分来之互联网,如有雷同,纯属必然。 可以加群讨论,群里有各种源码,资料: 215269573

winform摄像头拍照 C#利用摄像头拍照的更多相关文章

  1. Winform开发框架之通用Windows摄像头调用拍照--SNF快速开发平台3.3-Spring.Net.Framework

    今天做了一个windows系统下调用摄像头.进行开启.关闭.拍照.设置等等功能演示. 进行源码贡献,欢迎大家下载使用 一.DEMO效果如下: 二.DEMO演示代码如下: using SNF.Utili ...

  2. 【Android】自己定义相机的实现(支持连续拍照、前后摄像头切换、连续对焦)

    ~转载请注明http://blog.csdn.net/u013015161/article/details/46921257 介绍 这几天.写了一个自己定义照相机的demo.支持连续拍照和摄像头切换. ...

  3. 项目实战:Qt+Ffmpeg+OpenCV相机程序(打开摄像头、支持多种摄像头、分辨率调整、翻转、旋转、亮度调整、拍照、录像、回放图片、回放录像)

    若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...

  4. Java乔晓松-android中调用系统拍照功能并显示拍照的图片

    android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...

  5. 教你如何认识人脸识别开发套件中的双目摄像、3D结构光摄像头、单目摄像头的区别及详细讲解

    深圳市宁远电子提供的人脸识别模组可支持双目摄像头和3D结构光摄像头,在客户咨询中经常有被问到双目的为什么会比单目的成本高,区别在哪里,他们的适用于哪些场景呢?在此,深圳市宁远电子技术工程师就为大家详细 ...

  6. Android Camera开发系列(上)——Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片

    Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 最近也是在搞个破相机,兼容性那叫一个不忍直视啊,于是自己翻阅了一些基本的资料,自己实现了一 ...

  7. 英特尔® 实感™ 深度摄像头代码示例 – R200 摄像头数据流

    英特尔开发人员专区原文地址 简介 该可下载代码示例展示了如何使用面向 Windows 的英特尔® 实感™ SDK* 捕捉和查看用 C#/XAML 编写的原始 R200 摄像头数据流. Visual S ...

  8. [转载]树莓派新版系统上使用mjpg-streamer获取USB摄像头和树莓派专用摄像头RaspiCamera图像

    树莓派新版系统上使用mjpg-streamer获取USB摄像头和树莓派专用摄像头RaspiCamera图像 网上有很多关于mjpg-stream移植到树莓派的文章,大部分还是使用的sourceforg ...

  9. C#利用摄像头拍照功能实现

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

随机推荐

  1. java2实用教程102小程序(分数计算和流水线计算

    import java.util.Scanner; public class test{ public static void main(String args[]){ Rational a=new ...

  2. JavaScript 本地对象、内置对象、宿主对象

    首先解释下宿主环境:一般宿主环境由外壳程序创建与维护,只要能提供js引擎执行的环境都可称之为外壳程序.如:web浏览器,一些桌面应用系统等.即由web浏览器或是这些桌面应用系统早就的环境即宿主环境. ...

  3. thinkphp 配置

    ThinkPHP框架中所有配置文件的定义格式均采用返回PHP数组的方式,格式为: //项目配置文件 return array( 'DEFAULT_MODULE' => 'Index', //默认 ...

  4. JAVA-POI实现EXCEL的读写

    想要完成JAVA读写EXCEL,首先需要JAVA-POI包的支持,百度搜索即可找到资源,不再赘述: POI-新增EXCEL并输入内容 package com.gsh.test.poi; import ...

  5. php的冒泡算法

    <?php /* 冒泡算法  * @para $arr 传人进去排序的数组  * @return $newArr 排序之后的数组  */   function maopao($arr){     ...

  6. php 产生不重复的随机数

    $arr=array();//创建数组 while(count($arr)<10){ $a = mt_rand(1000,9999);//产生随机数 if(!in_array($a,$arr)) ...

  7. js限制textarea文本框的文字个数

    现在发微博,那个文本框一般只能输入200字好像,再多就会自动删除,要么是提示字数受限,用Js就可实现本功能.今天带来的这个Js限制表单文本 框文字数量的例子,相信有此方面需要的是个不错的参考.为了便于 ...

  8. 搭建splinter+python环境时遇到的错误

    因为不想用urllib2了,没有用过splinter,今天就想试试,毕竟后者支持的功能更人性化/自动化. 1,安装splinter 安装过程很简单,安装了pip的话,执行: $ [sudo] pip ...

  9. 文本阴影:text-shadow

    例如: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  10. Altium Designer 导出Gerber文件详细教程

    Altium Designer 导出Gerber文件详细教程   1.用Altium打开需要导出Gerber文件的PCB: 2.点击“File”-“fabricatio Outputs ” “Gerb ...