记得先引用DLL

private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource; BLL.AWBListBLL alb = new AWBListBLL();
public FormPhoto()
{
InitializeComponent(); //try
//{
// // 枚举所有视频输入设备
// videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); // if (videoDevices.Count == 0)
// throw new ApplicationException(); // foreach (FilterInfo device in videoDevices)
// {
// tscbxCameras.Items.Add(device.Name);
// } // tscbxCameras.SelectedIndex = 0; //}
//catch (ApplicationException)
//{
// tscbxCameras.Items.Add("No local capture devices");
// videoDevices = null;
//}
//CameraConn(); }
public FormPhoto(string ShipCode,bool PrintView,int PhotoFlag)
{
InitializeComponent();
//try
//{
// // 枚举所有视频输入设备
// videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); // if (videoDevices.Count == 0)
// throw new ApplicationException(); // foreach (FilterInfo device in videoDevices)
// {
// tscbxCameras.Items.Add(device.Name);
// } // tscbxCameras.SelectedIndex = 0; //}
//catch (ApplicationException)
//{
// tscbxCameras.Items.Add("No local capture devices");
// videoDevices = null;
//} //shipcode = ShipCode;
//printpreview = PrintView;
//photoFlag = PhotoFlag;
//this.btnPhoto.Focus();
} //连接摄像头
private void CameraConn()
{
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);
videoSource.DesiredFrameSize = new System.Drawing.Size(919, 718);
videoSource.DesiredFrameRate = 1; videoSourcePlayer.VideoSource = videoSource;
videoSourcePlayer.Start();
} public FormPhoto(string sgcode,int photoflag)
{
InitializeComponent(); try
{
// 枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); if (videoDevices.Count == 0)
throw new ApplicationException(); foreach (FilterInfo device in videoDevices)
{
tscbxCameras.Items.Add(device.Name);
} tscbxCameras.SelectedIndex = 0; }
catch (ApplicationException)
{
tscbxCameras.Items.Add("No local capture devices");
videoDevices = null;
}
CameraConn();
photoFlag = photoflag;
shipcode = sgcode;
this.btnPhoto.Focus();
}
//public void startVideo()
//{
// video.StartVideo();
// previewPictureBox.Visible = true;
// showPictureBox.Visible = false;
//} private void btnPhoto_Click(object sender, EventArgs e)
{
try
{
string pathName = System.Windows.Forms.Application.StartupPath + "\\PhotoImage\\" + shipcode + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
//保存图片
//video.Images(pathName);
////关闭摄像头
//video.StopVideo();
//showPictureBox.ImageLocation = pathName;
//previewPictureBox.Visible = false;
//showPictureBox.Visible = true;
if (videoSourcePlayer.IsRunning)
{
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
videoSourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
IntPtr.Zero,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
PngBitmapEncoder pe = new PngBitmapEncoder();
pe.Frames.Add(BitmapFrame.Create(bitmapSource));
using (Stream stream = File.Create(pathName))
{
pe.Save(stream);
}
videoSourcePlayer.Visible = false;
//拍照完成后关摄像头并刷新同时关窗体
if (videoSourcePlayer != null && videoSourcePlayer.IsRunning)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
}
pictureBox1.Visible = true;
pictureBox1.ImageLocation = pathName;
DialogResult result = System.Windows.Forms.MessageBox.Show("Whether or not to save?", "The system prompt", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//将图像读入到字节数组
//System.IO.FileStream fs = new System.IO.FileStream(pathName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
//byte[] buffByte = new byte[fs.Length];
//fs.Read(buffByte, 0, (int)fs.Length);
//fs.Close();
//fs = null;
FileStream fs = new FileStream(pathName, FileMode.Open);
Bitmap bmp = new Bitmap(fs);
fs.Close();
byte[] buffByte = ImageGdi(bmp); //根据包裹号查运单号
DataSet ds = new DataSet();
ds = alb.SelectfegAWBListBySlShipCode1(shipcode);
if (ds.Tables[0].Rows.Count > 0)
{
imagemd.Sl_ShipNo = ds.Tables[0].Rows[0]["slShipCode"].ToString();
}
imagemd.Sl_Code = shipcode;
imagemd.ImageName = pathName.Substring(pathName.LastIndexOf("\\") + 1);
imagemd.Sl_Image = buffByte;
//将信息保存到数据库
DataSet ds1 = imagebll.SelectfegIamge2(shipcode);
if (ds1.Tables[0].Rows.Count > 0)
{
imagebll.UpdatefegImage(imagemd);
//修改以后 删除本地上张图片.... }
else
{
imagebll.InsertfegImage(imagemd);
}
SaveMessage.Text = "Save success!";
//判断是入库操作或者是打印面单操作 0为入库操作 1为打印面单操作
//if (photoFlag == 1)
//{
// //打印面单保存成功后跳转到打印页面
// UI.FormExpress express = new UI.FormExpress(shipcode, printpreview);
// express.Show();
// this.Close();
//}
//if (photoFlag == 0)
//{
// //入库操作 保存图片后直接关闭
// this.Close();
//}
//想了想还是删除照片 不然占内存大
System.IO.FileInfo file = new System.IO.FileInfo(pathName);
if (file.Exists)
{
file.Delete();
}
this.Close();
//读取二进制图片,待用到时再用。先保存在这里
//SqlCommand cmd = new SqlCommand("select 照片 from fuser where password='1b'", conn);
//SqlDataReader reader = cmd.ExecuteReader();
//reader.Read();
//MemoryStream buf = new MemoryStream((byte[])reader[0]);
//Image image = Image.FromStream(buf, true);
//pictureBox1.Image = image; }
else
{
//删除照片,重新拍
System.IO.FileInfo file = new System.IO.FileInfo(pathName);
if (file.Exists)
{
file.Delete();
}
pictureBox1.Visible = false;
videoSourcePlayer.Visible = true;
//startVideo();
CameraConn();
this.btnPhoto.Focus();
}
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("摄像头异常:" + ex.Message);
}
} private void btnConnect_Click(object sender, EventArgs e)
{
CameraConn();
} private void button1_Click(object sender, EventArgs e)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
} ///
/// GDI压缩图片
///
/// 传入参数Bitmap
///
public byte[] ImageGdi(Bitmap bmp)
{
Bitmap xbmp = new Bitmap(bmp);
MemoryStream ms = new MemoryStream();
xbmp.Save(ms, ImageFormat.Jpeg);
byte[] buffer;
ms.Flush();
if (ms.Length > 95000)
{
//buffer = ms.GetBuffer();
double new_width = 0;
double new_height = 0; Image m_src_image = Image.FromStream(ms);
if (m_src_image.Width >= m_src_image.Height)
{
new_width = 1024;
new_height = new_width * m_src_image.Height / (double)m_src_image.Width;
}
else if (m_src_image.Height >= m_src_image.Width)
{
new_height = 768;
new_width = new_height * m_src_image.Width / (double)m_src_image.Height;
} Bitmap bbmp = new Bitmap((int)new_width, (int)new_height, m_src_image.PixelFormat);
Graphics m_graphics = Graphics.FromImage(bbmp);
m_graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
m_graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
m_graphics.DrawImage(m_src_image, 0, 0, bbmp.Width, bbmp.Height); ms = new MemoryStream(); bbmp.Save(ms, ImageFormat.Jpeg);
buffer = ms.GetBuffer();
ms.Close(); return buffer;
}
else
{
buffer = ms.GetBuffer();
ms.Close();
return buffer;
}
} private void FormPhoto_FormClosing(object sender, FormClosingEventArgs e)
{
videoSourcePlayer.SignalToStop();
videoSourcePlayer.WaitForStop();
} // private byte[] CompressionImage(Stream fileStream, long quality)
//{
// using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream))
// {
// using (Bitmap bitmap = new Bitmap(img))
// {
// System.Drawing.Imaging.ImageCodecInfo CodecInfo = GetEncoder(img.RawFormat);
// System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
// EncoderParameters myEncoderParameters = new EncoderParameters(1);
// EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, quality);
// myEncoderParameters.Param[0] = myEncoderParameter;
// using (MemoryStream ms = new MemoryStream())
// {
// bitmap.Save(ms, CodecInfo, myEncoderParameters);
// myEncoderParameters.Dispose();
// myEncoderParameter.Dispose();
// return ms.ToArray();
// }
// }
// }
//}

AForge实现拍照的更多相关文章

  1. AForge.net 录像拍照功能实现 转

    AForge.net 使用之录像拍照功能实现 最近使用aforge.NET拍照录像功能实现 记录一下以便以后好学习,哈哈,直接上代码 连接摄像头设备,这里需要引入 AForge.Video; AFor ...

  2. C# 使用AForge调用笔记本摄像头拍照

    vs2012  winform 连接摄像头设备,这里需要引入 代码: using AForge; using AForge.Controls; using AForge.Imaging; using ...

  3. 在WPF中使用AForge.net控制摄像头拍照

    原文:在WPF中使用AForge.net控制摄像头拍照 利用AForge.net控制摄像头拍照最方便的方法就是利用PictureBox显示摄像头画面,但在WPF中不能直接使用PictureBox.必须 ...

  4. AForge.net 使用之录像拍照功能实现

    连接摄像头设备,这里需要引入 AForge.Video; AForge.Video.DirectShow; AForge.Video.FFMPEG; 还需要添加引用,aforge.dll,aforge ...

  5. 利用AForge.NET 调用电脑摄像头进行拍照

    当然了,你需要去官网下载类库,http://www.aforgenet.com/ 调用本机摄像头常用的组件: AForge AForge.Controls AForge.Imaging AForge. ...

  6. C#使用Aforge调用摄像头拍照

    一. 新建一个Winform项目 二.使用Nuget添加引用 安装下图中红色框住的两个程序包 安装完后发现安装了如下图的程序包,这是因为上述两个程序包存在对其它程序包的依赖. 三.编写程序 1. 窗体 ...

  7. C# - VS2019调用AForge库实现调用摄像头拍照功能

    前言 作为一名资深Delphi7程序员,想要实现摄像头扫描一维码/二维码功能,发现所有免费的第三方库都没有简便的实现办法,通用的OpenCV或者ZXing库基本上只支持XE以上的版本,而且一维码的识别 ...

  8. WinForm利用AForge.NET调用电脑摄像头进行拍照和视频

    当然了,你需要去官网下载类库,http://www.aforgenet.com/ 调用本机摄像头常用的组件: AForge AForge.Controls AForge.Imaging AForge. ...

  9. AForge调用摄像头拍照时设置分辨率

    简单记录下AForge2.2.5.0版本调用摄像头拍照时设置分辨率的方法. FilterInfo info = _videoDevices[0];//获取第一个摄像头 _cameraDevice = ...

随机推荐

  1. Python 学习博客地址

    Alex  https://www.cnblogs.com/alex3714林海峰 https://www.cnblogs.com/linhaifeng武佩奇 https://www.cnblogs. ...

  2. Leetcode】周赛203 查找大小为M的最新分组

    题意: 给你一个数组 arr ,该数组表示一个从 1 到 n 的数字排列.有一个长度为 n 的二进制字符串,该字符串上的所有位最初都设置为 0 . 在从 1 到 n 的每个步骤 i 中(假设二进制字符 ...

  3. HDU -1506 Largest Rectangle in a Histogram&&51nod 1158 全是1的最大子矩阵 (单调栈)

    单调栈和队列讲解:传送门 HDU -1506题意: 就是给你一些矩形的高度,让你统计由这些矩形构成的那个矩形面积最大 如上图所示,如果题目给出的全部是递增的,那么就可以用贪心来解决 从左向右依次让每一 ...

  4. 前端模块化之ES Module

    一.概述 之前提到的几种模块化规范:CommonJS.AMD.CMD都是社区提出的.ES 2015在语言层面上实现了模块功能,且实现简单,可以替代CommonJS和AMD规范,成为在服务器和浏览器通用 ...

  5. Spring Cloud实战 | 第十一篇:Spring Cloud Gateway 网关实现对RESTful接口权限控制和按钮权限控制

    一. 前言 hi,大家好,这应该是农历年前的关于开源项目 的最后一篇文章了. 有来商城 是基于 Spring Cloud OAuth2 + Spring Cloud Gateway + JWT实现的统 ...

  6. Nginx 四层负载均衡

    目录 四层负载均衡概述 配置七层负载均衡 配置四层负载均衡 四层负载均衡概述 四层负载均衡是基于IP+端口的负载均衡,七层负载均衡是基于URL或主机名等应用层信息的负载均衡. 其他层负载均衡(转载): ...

  7. 鸟哥的linux私房菜——第九章学习(vim编辑器)

    第九章vim编辑器 1.0).vi与vim Linux下文本界面的文书编辑器通常会有常常听到的就有: emacs, pico, nano, joe, 与 vim 等等. vi的优势: 所有的 Unix ...

  8. C# 类 (8) - 抽象方法

    抽象 抽象方法 只能 定义在抽象类 里,并且抽象方法里没有具体的代码,像这种 为啥要定义一个空空如也的函数呢?这是为了用来约束 它的派生类 的行为, 这个例子,建立了一个数组,放了cat和dog,这两 ...

  9. spring-cloud-netflix-hystrix-turbine

    Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数 ...

  10. 全方位构造免杀 webshell 小结[一]

    转载自https://klionsec.github.io/2017/10/11/bypasswaf-for-webshell/   全方位构造免杀 webshell 小结[一]   前言:    本 ...