由于一直在高校工作,就涉及到招生工作,招生时候又要收集学生图像采集,所以就随手写了一个图像采集工具,废话不多说,进入正题。

图像采集需要调用摄像头就行拍照操作,网上查了一下资料,需要引用以下3个dll。

看一下运行界面

界面都比较low,主要是功能实现。

        private void Camera_Load(object sender, EventArgs e)
{
this.btnSave.Enabled = false;
try
{
borderSize = GetBorderSize(this);
captionHeight = GetCaptionHeight(this);
//InitStudent("", "", ""); this.comboBox_SizeMode.Text = "填充(保持比例)";
FilterInfoCollection infos = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if ((infos != null) && (infos.Count > ))
{ }
else
{
MessageBox.Show("没有视频设备");
} this.LoadVedio(); this.splitContainer1.Panel2MinSize = ;
this.splitContainer1.SplitterDistance = this.splitContainer1.Width - this.splitContainer1.Panel2MinSize + ; mf = new BorderForm(); mf.Show(this);
//mf.Left = (this.Left + this.splitContainer1.Panel1.Width - mf.Width) / 2;
//mf.Top = (this.Top + this.splitContainer1.Panel1.Height - mf.Height) / 2;
//marLeft = mf.Left - this.Left;
//marTop = mf.Top - this.Top; Rectangle rtPic = this.pictureBox_Camera.RectangleToScreen(this.pictureBox_Camera.ClientRectangle);
Rectangle rtMF = this.mf.RectangleToScreen(this.mf.ClientRectangle);
if (rtPic == null || rtMF == null || rtPic.Width == || rtMF.Width == )
{
return;
}
mf.Left = ((rtMF.Width + rtMF.Left) + (rtPic.Width + rtPic.Left)) / ;
mf.Top = ((rtMF.Height + rtMF.Top) + (rtPic.Height + rtPic.Top)) / ; mf.SizeChanged += new EventHandler(mf_SizeChanged);
mf.LocationChanged += new EventHandler(mf_LocationChanged); pictureBox_Camera_SizeChanged(sender, e); //启动连拍
//this.ShootOneTime = 0;
this.timer1.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//加载摄像头设备
private void LoadVedio()
{
FilterInfoCollection infos = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if ((infos != null) && (infos.Count > ))
{
int index = ;
foreach (FilterInfo info in infos)
{
this.cmbCaptureDevice.Items.Add(new DeviceInfo(info.Name, info.MonikerString, index, FilterCategory.VideoInputDevice));
index++;
}
this.cmbCaptureDevice.SelectedIndex = ;
}
}
        /// <summary>
/// 拍照
/// </summary>
private void Shoot()
{
try
{
if (this.pictureBox_Camera.Image != null && (int)this.numericUpDown1.Value > && (int)this.numericUpDown2.Value > )
{
Bitmap resultImage = new Bitmap((int)this.numericUpDown1.Value, (int)this.numericUpDown2.Value);
Graphics g = Graphics.FromImage(resultImage);
g.CopyFromScreen(new Point(this.mf.Location.X + , this.mf.Location.Y + ), new Point(, + (isWin7 ? : )), new Size(resultImage.Size.Width, resultImage.Size.Height - ( + (isWin7 ? : ))));
if (!string.IsNullOrEmpty(XH))
{
string str = "";
if (this.XH != "")
{
str = this.XH;
}
else if (this.SFZH != "")
{
str = this.SFZH;
}
else if (this.KSH != "")
{
str = this.KSH;
}
if (this.checkBox2.Checked)
{
str = XM + " " + str;
}
int txtWidth = (int)(g.MeasureString(str, new Font("宋体", )).Width * 1.1);
Rectangle rec = new Rectangle((resultImage.Width - txtWidth) / , resultImage.Height - , txtWidth, );
g.FillRectangle(Brushes.White, rec);
StringFormat sf = new StringFormat();
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
rec.Height++;
g.DrawString(str, new Font("宋体", ), Brushes.Black, rec, sf);
}
this.pictureBox_tx.Image = resultImage;
}
else
{
this.pictureBox_tx.Image = null;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//选择摄像装置
private void cmbCaptureDevice_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.cmbCaptureDevice.SelectedItem != null)
{
this.cmbDeviceCapability.Items.Clear();
VideoCaptureDevice device = new VideoCaptureDevice(((DeviceInfo)this.cmbCaptureDevice.SelectedItem).MonikerString);
for (int i = ; i < device.VideoCapabilities.Length; i++)
{
VideoCapabilities capabilities = device.VideoCapabilities[i];
DeviceCapabilityInfo item = new DeviceCapabilityInfo(capabilities.FrameSize);
this.cmbDeviceCapability.Items.Add(item);
}
DeviceInfo selectedItem = (DeviceInfo)this.cmbCaptureDevice.SelectedItem;
if (this.captureAForge != null)
{
this.captureAForge.NewFrame -= new NewFrameEventHandler(this.captureAForge_NewFrame);
//this.captureAForge.SnapshotFrame -= new NewFrameEventHandler(this.captureAForge_SnapshotFrame);
if (this.captureAForge.IsRunning)
{
this.captureAForge.SignalToStop();
}
this.captureAForge.WaitForStop();
this.captureAForge = null;
}
this.captureAForge = new VideoCaptureDevice(selectedItem.MonikerString);
this.captureAForge.ProvideSnapshots = true;
this.captureAForge.NewFrame += new NewFrameEventHandler(this.captureAForge_NewFrame);
//this.captureAForge.SnapshotFrame += new NewFrameEventHandler(this.captureAForge_SnapshotFrame);
if (this.cmbDeviceCapability.Items.Count > )
{
this.cmbDeviceCapability.SelectedIndex = ;
}
}
}
//选择分辨率
private void cmbDeviceCapability_SelectedIndexChanged(object sender, EventArgs e)
{
string[] strArray = this.cmbDeviceCapability.Text.Trim().Split(new char[] { 'x' });
int width = int.Parse(strArray[]);
int height = int.Parse(strArray[]);
if (this.captureAForge != null)
{
if (this.captureAForge.IsRunning)
{
this.captureAForge.SignalToStop();
}
this.captureAForge.WaitForStop();
this.captureAForge.DesiredFrameSize = new Size(width, height);
this.captureAForge.DesiredSnapshotSize = new Size(width, height); //this.captureAForge.DesiredFrameRate = 1000; this.captureAForge.Start(); }
}
//设置数据源大小
private void comboBox_SizeMode_SelectedIndexChanged(object sender, EventArgs e)
{
switch (this.comboBox_SizeMode.Text)
{
case "默认(原始大小)":
this.pictureBox_Camera.SizeMode = PictureBoxSizeMode.Normal;
break;
case "居中(原始大小)":
this.pictureBox_Camera.SizeMode = PictureBoxSizeMode.CenterImage;
break;
case "填充(拉伸图像)":
this.pictureBox_Camera.SizeMode = PictureBoxSizeMode.StretchImage;
break;
case "填充(保持比例)":
this.pictureBox_Camera.SizeMode = PictureBoxSizeMode.Zoom;
break;
}
}
//保存照片
private void buttonSave_Click(object sender, EventArgs e)
{
try
{
string filename = Path.Combine(Application.StartupPath, "StuImages", "Newstuimages", this.XH + ".JPG");
if (!this.checkBox1.Checked && File.Exists(filename))
{
if (MessageBox.Show("该生已经有照片文件,是否覆盖? ", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
{
//存储到本地
this.pictureBox_tx.Image.Save(filename);
//OnDataChanged(this, new DataEventArgs(string.Format("头像采集(覆盖),学号:{0},姓名:{1},考生号:{2},身份证号:{3}", this.XH, this.XM, this.KSH, this.SFZH)));
this.txtKSH.Focus();
this.txtKSH.SelectAll();
}
else
{
return;
}
}
else
{
this.pictureBox_tx.Image.Save(filename);
//OnDataChanged(this, new DataEventArgs(string.Format("头像采集,学号:{0},姓名:{1},考生号:{2},身份证号:{3}", this.XH, this.XM, this.KSH, this.SFZH)));
this.txtKSH.Focus();
this.txtKSH.SelectAll();
}
this.timer1.Start();
}
catch
{ }
finally
{
//启用下面一行,点击保存后头像会立即启动刷新
//this.timer1.Start();
this.btnSave.Enabled = false;
}
}

保存照片可以选择自动覆盖同名照片或者在照片中加入水印效果。

操作时候可以自行拖动长方形的框进行选择拍照

源码下载:http://files.cnblogs.com/files/luoxiaozhao/Image_acquisitionForm.rar

请支持原创,转载请标明来源。

C#实现拍照并且存水印照片的更多相关文章

  1. WebForm水印照片

    水印照片需要的元素 绘制:1.画布2.画笔 样式 粗细 颜色3.画什么东西4.用什么字体画 大小5.位置 展示页面 <%@ Page Language="C#" AutoEv ...

  2. Cocos2d-x使用android拍照功能加载照片内存过大,通过另存照片尺寸大小解决

    使用2dx调用android拍照功能,拍照结束后在2dx界面显示拍照照片,如果不对照片做处理,会出现内存过大的问题,导致程序崩溃,如果仅仅另存拍照照片,则照片质量大小均下降,导致照片不够清晰,后来发现 ...

  3. jQuery+PHP+Mysql在线拍照和在线浏览照片

    本文用示例讲述了如何使用jQuery与PHP及Mysql结合,实现WEB版在线拍照.上传.显示浏览的功能,ajax交互技术贯穿本文始末,所以本文的读者要求具备相当熟悉jQuery及其插件使用和javs ...

  4. Android 从相冊获取近期拍摄的多张照片(获取相机拍照所存储的照片)

    转载请标明出处:http://blog.csdn.net/android_ls/article/details/39928519 在做公司项目时.遇到的需求:自己定义显示照片的网格视图,显示用户近期採 ...

  5. Python控制自己的手机摄像头拍照,并把照片自动发送到邮箱

    写在前面的一些P话: 今天这个案例,就是控制自己的摄像头拍照,并且把拍下来的照片,通过邮件发到自己的邮箱里.想完成今天的这个案例,只要记住一个重点:你需要一个摄像头 思路 通过opencv调用摄像头拍 ...

  6. Android 拍照或相册选择照片进行显示缩放位图 Demo

    拍照后直接使用 BitmapFactory.decodeStream(...) 进行创建 Bitmap 并显示是有问题的. Bitmap 是个简单对象,它只存储实际像素数据,也就是说,即使原始照片已压 ...

  7. Android开发 处理拍照完成后的照片角度

    private void initImageAngle(){ Bitmap imageBitmap = BitmapFactory.decodeFile(FilePathSession.getFace ...

  8. UWP开发之Template10实践二:拍照功能你合理使用了吗?(TempState临时目录问题)

    最近在忙Asp.Net MVC开发一直没空更新UWP这块,不过有时间的话还是需要将自己的经验和大家分享下,以求共同进步. 在上章[UWP开发之Template10实践:本地文件与照相机文件操作的MVV ...

  9. HTML5调用手机相机拍照

    前端调用手机相机拍照 实现方式常见有两种: 一种是通过video控件,通过捕获video的流,截取video中的图像实现拍照, 还有一种是通过input[file]控件调用移动端的摄像头,实现拍照. ...

随机推荐

  1. msql数据库基础

    一.数据库操作 1.显示数据库 SHOW DATABASES; SHOW CREATE DATABASE 数据库名称; #数据库的创建信息 2.创建数据库 #utf8 CREATE DATABASE ...

  2. react 组件的构造函数

    constructor 函数时组件最先执行的函数 class childen extends react.Component{ constructor(props){ super(props); th ...

  3. 通过statCounter计算给定的RDD[Double]的统计信息的方法

    需求1:给定一个RDD[Double],进行计算,该RDD的统计信息(count,mean,stdev,max,min) 代码: def main(args: Array[String]): Unit ...

  4. leetcode-回溯

    题17: 方法一:回溯 class Solution: def letterCombinations(self, digits: str) -> List[str]: res = [] dic ...

  5. leetcood学习笔记-235-二叉搜索树的最近公共祖先

    题目描述: 利用二叉搜索树的特点,如果p.q的值都小于root,说明p q 肯定在root的左子树中:如果p q都大于root,说明肯定在root的右子树中,如果一个在左一个在右 则说明此时的root ...

  6. 深入浅出的Object.defineProperty()

    vue的原理 观察者模式和订阅-发布者模式. Vue实例被创建时,会遍历data属性,并通过Object.defineProperty将 这些属性转化为getter/setter,并进行追踪依赖.每当 ...

  7. thinkphp 模块部署

    3.2对模块的访问是自动判断的,所以通常情况下无需配置模块列表即可访问,在部署模块的时候,默认情况下都是基于类似于子目录的URL方式来访问模块的,例如: http://serverName/Home/ ...

  8. 矩阵乘法分配律+bitset优化——hdu4920

    因为是模3,所以把原矩阵拆成两个01矩阵,然后按分配律拆开分别进行矩阵乘法,行列用bitset来存进行优化即可 注意 int bitset<int>::count() 函数可以统计bits ...

  9. NX二次开发-创建旋转UF_MODL_create_revolved

    NX9+VS2012 #include <uf.h> #include <uf_curve.h> #include <uf_modl.h> UF_initializ ...

  10. JsJquery小技巧

    JS对URL编码 :encodeURI() .Net对URL解码:HttpUtility.UrlDecode() 格式化输出百分数 function formatePercent(data){     ...