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

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

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

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

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

下面开始进入程序

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

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

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

代码部分

  private int hHwnd = ;
private const int port = ;
private DataTable dtExcel;
private string imgName;
public CameraForm()
{
InitializeComponent();
}
public struct videohdr_tag
{
public byte[] lpData;
public int dwBufferLength;
public int dwBytesUsed;
public int dwTimeCaptured;
public int dwUser;
public int dwFlags;
public int[] dwReserved; }
  ///   <summary>
/// 必需的设计器变量。
/// </summary> [DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
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);
[DllImport("avicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool capGetDriverDescriptionA(short wDriver, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszName, int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref string lpszVer, int cbVer);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool DestroyWindow(int hndw);
[DllImport("user32", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SendMessage(int hwnd, int wMsg, int wParam, [MarshalAs(UnmanagedType.AsAny)] object lParam);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int SetWindowPos(int hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("vfw32.dll")]
public static extern string capVideoStreamCallback(int hwnd, videohdr_tag videohdr_tag);
[DllImport("vicap32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

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

         /// <summary>
/// 开启摄像头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
if (hHwnd == )
this.OpenCapture();
else
MessageBox.Show("摄像头已经开启!","提示");
}
      /// <summary>
/// 打开摄像头
/// </summary>
private void OpenCapture()
{
int intWidth = this.panel1.Width;
int intHeight = this.panel1.Height;
int intDevice = ;
string refDevice = intDevice.ToString();
//创建视频窗口并得到句柄
hHwnd = CameraForm.capCreateCaptureWindowA(ref refDevice, , , , , , this.panel1.Handle.ToInt32(), );
if (CameraForm.SendMessage(hHwnd, 0x40a, intDevice, ) > )
{
CameraForm.SendMessage(this.hHwnd, 0x435, -, );
CameraForm.SendMessage(this.hHwnd, 0x434, 0x42, );
CameraForm.SendMessage(this.hHwnd, 0x432, -, );
CameraForm.SetWindowPos(this.hHwnd, , , , intWidth, intHeight, );
}
else
{
CameraForm.DestroyWindow(this.hHwnd);
}
}

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

接下来关闭摄像头

         /// <summary>
///关闭摄像头
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
//停止视频注销视频句柄
CameraForm.SendMessage(this.hHwnd, 0x40b, , );
CameraForm.DestroyWindow(this.hHwnd);
hHwnd = ;
}

然后是拍照

   /// <summary>
/// 截图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (hHwnd == )
{
MessageBox.Show("请先开启摄像头!","提示");
return;
}
Preview dlg = new Preview();
try
{
CameraForm.SendMessage(this.hHwnd, 0x41e, , );
IDataObject obj1 = Clipboard.GetDataObject();
if (obj1.GetDataPresent(typeof(Bitmap)))
{
Rectangle rect = new Rectangle(, , , );
Image image1 = (Image)obj1.GetData(typeof(Bitmap));
Bitmap bit = (Bitmap)image1;
//MessageBox.Show( bit.Height.ToString());
//MessageBox.Show(bit.Width.ToString()); bit = bit.Clone(new Rectangle(, , , ), System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bit = (Bitmap)GetThumbnailImage(bit, , );
dlg.Img = bit;
dlg.ImgName = imgName;
}
}
catch
{
}
dlg.ShowDialog();
}
  /// <summary>
/// 将某个图像生成指定尺寸的图像缩放图
/// </summary>
/// <param name="myBitmap">原图像</param>
/// <param name="width">新的图像宽</param>
/// <param name="height">新的图像高</param>
/// <returns></returns>
private Image GetThumbnailImage(Bitmap myBitmap, int width, int height)
{ //生成图像的缩放图
Image.GetThumbnailImageAbort myCallback =
new Image.GetThumbnailImageAbort(ThumbnailCallback); Image myThumbnail = myBitmap.GetThumbnailImage(
width, height, myCallback, IntPtr.Zero);
//this.pictureBox1.Image = myThumbnail; return myThumbnail;
}
public bool ThumbnailCallback()
{
return false;
}

最后是保存截图

  private void button1_Click(object sender, EventArgs e)
{
SaveFileDialog SaveFileDialog1 = new SaveFileDialog();
SaveFileDialog1.FileName = imgName;
SaveFileDialog1.Filter = "Image Files(*.JPG;*.GIF)|*.JPG;*.GIF|All files (*.*)|*.*";
if (SaveFileDialog1.ShowDialog() == DialogResult.OK)
{
img.Save(SaveFileDialog1.FileName, ImageFormat.Bmp);
this.Close();
}
}

下面是操作Excel

首先判断Excel版本

  /// <summary>
/// 检测Excel版本信息
/// </summary>
/// <returns></returns>
public static double JongCheckExcelVer()
{
Type objExcelType = Type.GetTypeFromProgID("Excel.Application");
if (objExcelType == null)
{
return ;
}
object objApp = Activator.CreateInstance(objExcelType);
if (objApp == null)
{
return ;
}
object objVer = objApp.GetType().InvokeMember("Version", BindingFlags.GetProperty, null, objApp, null);
double iVer = Convert.ToDouble(objVer.ToString());
objVer = null;
objApp = null;
objExcelType = null;
GC.Collect();
return iVer;
} public static String JongGetExcelVerStr()
{
String s1;
double excelver;
excelver = JongCheckExcelVer();// ExistsExcelRegedit();
s1 = " Office ";
if (excelver == )
{
MessageBox.Show("無法識別Excel的版本", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Information);
s1 = "無法識別 office 版本";
}
else if (excelver >= ) s1 += "2010或以上";
else if (excelver >= ) s1 += "";
else if (excelver >= ) s1 += "";
else if (excelver >= ) s1 += "XP";
else if (excelver >= ) s1 += "";
else if (excelver >= ) s1 += "";
else if (excelver >= ) s1 += ""; return s1;
}

接着是导入到DataTable

   /// <summary>
/// 导入excel到datata
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
try
{
if (fd.ShowDialog() == DialogResult.OK)
{
DataSet ds = new DataSet(); string strConn = ""; if (JongCheckExcelVer() >= )
{
strConn = "Provider=Microsoft.Ace.OLEDB.12.0;" + "Data Source=" + fd.FileName + ";" + "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'";
}
else if (JongCheckExcelVer() >= )
{
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fd.FileName + ";" + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'";
}
else if (JongCheckExcelVer() == )
{
MessageBox.Show("无法识别Excel的版本", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
} OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
//string strSheet = "";
string strSheetName = "";
OleDbDataAdapter myCommand = null;
DataTable dtExcelName = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); strSheetName = dtExcelName.Rows[][].ToString(); strExcel = string.Format("select * from [{0}$]", "Sheet1"); //Bicycle为excel中工作薄
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds, "Sheet1");
System.Data.DataTable dt = new System.Data.DataTable();
dt = ds.Tables[];
dtExcel = dt;
MessageBox.Show("导入成功!","提示");
}
else
MessageBox.Show("导入失败!","提示");
}
catch
{
MessageBox.Show("请选择相应的excel文件", "警告");
}
}

本程序代码大部分来之互联网,如有雷同,纯属必然。 可以加群讨论,群里有各种源码,资料: 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. 控制弹出div显示在鼠标附近的位置

    前一个页面: $("#txt_ocname").click(function () { art.dialog.open("/SelPosAll.aspx", { ...

  2. c#之线程入门

    C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为“主线程”)自动创建的,并具有多线程创建额 ...

  3. Mysql 查询性能优化

    查询优化,索引优化,库表结构优化需要齐头并进,一个不能落. 为什么查询速度会慢 在阐释编写快速的查询之前,需要清楚一点,真正重要的是响应时间.如果把查询看做是一个任务的话,那么它由一系列子任务构成,每 ...

  4. 正则-匹配获取url参数

    1.根据指定参数名获取参数值 A页面向连接到B页面的url为: http://www.189dg.com/ajax/sms_query.ashx?action=smsdetail&sid=22 ...

  5. 在线支付接口之PHP支付宝接口开发简单介绍

    php100:92:在线支付接口之PHP支付宝接口开发 支付接口一般是第三方提供的代收款.付款的平台,可以通过支付接口帮助企业或个人利用一切可以使用的支付方式.常见支付平台:支付宝.快钱.云网支付.财 ...

  6. Delphi XE7下 Intraweb 发布为ASP.NET应用程序

    一.XE7下Intraweb开发这里就不说了,重点是在开发Intraweb时与ISAPI不同之处要选择 IW library,编译成DLL文件. 二.网站的配置 1.这是站点的物理路径,c:\site ...

  7. css、js的相互阻塞

    先决条件:脚本前面存在外部样式 以下试验虽然是在chrome下,但是对于IE8+以及其他浏览器也适用. 1.内联脚本(http://jsbin.com/mudab/1) <!DOCTYPE ht ...

  8. Ubuntu12.04 下安装Chrome浏览器

    第一步 下载. 32位:https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb 64位:https://dl. ...

  9. RAILS ON

    我是按照下面这个URL来轻快安装的. http://lxiaodao.iteye.com/blog/1579992 (1)RVM官方网站应该是改版过一次, 使用 curl -L https://get ...

  10. 什么是PWM、PFM及VFM

    做电源设计的大都知道PWM和PFM这两个概念.而VFM模式是在大功率輸出時為PWM模式在輕負載輸出時變為PFM模式的一種混合開關模式.目前开关电源的控制技术主要就是这三种:1.脉冲宽度调制器(PWM) ...