记得先引用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. 一条sql查出数据库某张表的所有属性

    select t.TABLE_NAME,--表名 t.COLUMN_NAME,--字段名 t.DATA_TYPE,--字段属性 t.DATA_LENGTH,--类型长度 t.DATA_PRECISIO ...

  2. mvn 多模块

    mvn archetype:generate -DgroupId=com.xxx.cloud -DartifactId=myapp -Dversion=1.0.0-SNAPSHOT -Dpackage ...

  3. php开发扩展环境的搭建(Windows)

    php开发扩展环境的搭建(Windows) 前期准备: (1) 下载php-5.3.10源码包(php-5.3.10.tar.bz2)并解压到C:\php-5.3.10:下载二进制包php-5.3.1 ...

  4. Nginx图文详解

    想必大家一定听说过 Nginx,若没听说过它,那么一定听过它的"同行"Apache 吧! Nginx 的产生 Nginx 同 Apache 一样都是一种 Web 服务器.基于 RE ...

  5. DoTween动画插件学习

    一.简单的变量插值运算 using System.Collections; using System.Collections.Generic; using UnityEngine; using DG. ...

  6. sentinel流控规则校验之源码分析

    前言: 上节给大家把sentinel流控整个执行大致过了,但涉及到最核心的流控算法还没有讲,先提前说明一下 sentinel用的流控算法是令牌桶算法,参考了Guava的RateLimiter,有读过R ...

  7. linux无需root挂载iso镜像文件

    引言 起初,我在针对deepin制作一款appimage安装工具,想要其实现的功能就是自动获取图标,只需要输入软件名称和分类即可,当然以后也会寻找方案省去手动输入的麻烦. 后来我发现一个有趣的问题 o ...

  8. .NET中使用DebuggerDisplay轻松定制调试

    前言 对于调试的方式有多种,不过在今天我们将看到的监视窗口对变量的监视,当然在这里我们是定制内部的变量值,或者说变量的显示与计算的内容. 注:监视窗口在调试时可以一次显示多个变量."快速监视 ...

  9. 2018ACM上海大都会赛 F Color it【基础的扫描线】

    题目:戳这里 题意:有n*m个点全为白色,q个圆,将q个圆内所有的点都染成黑色,问最后剩下多少白色的点. 解题思路:每一行当做一个扫描线,扫描所有的圆,记录每一行在圆中的点即可,O(n*q). 附ac ...

  10. Leetcode(712)-账户合并

    给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该帐户的邮箱地址 ...