一、在nuget程序集中添加如下库
二、项目代码如下
using AForge.Controls;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection videodevices;//所有摄像设备
private VideoCaptureDevice videodevice;//摄像设备
private Bitmap bitmap; //拍照缓存
string PicName; //照片保存缓存路径
private string PhotoSavePath=$"{Application.StartupPath}\\photo"; //照片保存路径
private void Form1_Load(object sender, EventArgs e)
{
videodevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); //查找本机设备端口
//得到所有接入的摄像设备
if (videodevices.Count != 0)
{
foreach (FilterInfo device in videodevices)
{
comboBox1.Items.Add(device.Name);//把摄像设备添加到摄像列表中
}
}
else
{
MessageBox.Show("没有找到摄像头!");
this.Close();
return;
}
comboBox1.SelectedIndex = 1; //选择为默认
videodevice = new VideoCaptureDevice(videodevices[comboBox1.SelectedIndex].MonikerString);
videoSourcePlayer1.VideoSource = videodevice;
//videoSourcePlayer1.SignalToStop();
//videoSourcePlayer1.WaitForStop();
videoSourcePlayer1.Start();
pictureBox1.SizeMode=PictureBoxSizeMode.StretchImage; //设置图片模式为拉伸
if (!Directory.Exists(PhotoSavePath)) //判断保存照片的文件夹是否存在
{
Directory.CreateDirectory(PhotoSavePath);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
videoSourcePlayer1.SignalToStop(); //关闭摄像头
// videoSourcePlayer1.Stop(); //关闭窗体时,释放摄像头资源
}
private void button1_Click(object sender, EventArgs e) //拍照
{
if (videoSourcePlayer1 == null) //防止摄像头未加载 报错
{
return;
}
bitmap = videoSourcePlayer1.GetCurrentVideoFrame(); //拍照
pictureBox1.Image = bitmap; //显示照片
pictureBox1.Image = PhotoAddWaterMark.Letterwatermark(bitmap, "水印:" + "Test" +DateTime.Now.ToString("yyyymmddhhmmss")+ "\r\n" , Color.Red, 1, 25, "黑体"); //显示照片并添加水印信息
}
private void button2_Click(object sender, EventArgs e) //保存照片
{
try
{
if (Directory.Exists(PhotoSavePath)) //判断文件夹是否存在
{
PicName = PhotoSavePath + "\\" + "Test" + DateTime.Now.ToString("yyyymmddhhmmss") + ".jpg";
bitmap.Save(PicName, pictureBox1.Image.RawFormat);
this.pictureBox1.Image = null;
bitmap.Dispose();
MessageBox.Show("保存成功");
}
else
{
MessageBox.Show("保存失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button3_Click(object sender, EventArgs e) //删除照片
{
DialogResult = MessageBox.Show("确认删除吗?", "请选择", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (DialogResult == DialogResult.OK)
{
this.pictureBox1.Image = null;
}
else
{
return;
}
}
private void button4_Click(object sender, EventArgs e) //打开文件夹
{
Process.Start("explorer.exe", PhotoSavePath); //通过进程打开目标文件夹
}
}
}
三、图片添加水印工具类如下
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp1
{
public class PhotoAddWaterMark
{
/// <summary>
/// 给照片添加水印
/// </summary>
/// <param name="img">需要添加水印的照片</param>
/// <param name="text">显示的文本</param>
/// <param name="color">文本字体颜色</param>
/// <param name="i">水印在图片上的位置</param>
/// <param name="fontsize">字体大小</param>
/// <param name="fontname">选择字体</param>
/// <returns>返回处理后的照片</returns>
public static Image Letterwatermark(Image img, string text, Color color, int i, float fontsize, string fontname)
{
Graphics gs = Graphics.FromImage(img);
Font font1 = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF crsizef;
crsizef = gs.MeasureString(text, font1);
float xpos = 0;
float ypos = 0;
switch (i)
{
case 1://左上角显示
xpos = (float)img.Width * (float).01;
ypos = (float)img.Height * (float).01;
break;
default:
break;
}
Brush brush = new SolidBrush(color);
gs.DrawString(text, font1, new SolidBrush(Color.White), xpos + 1, ypos + 1);
gs.DrawString(text, font1, brush, xpos, ypos);
gs.Dispose();
return img;
}
}
}
四、运行结果如下