当前位置:首页 > Windows程序 > 正文

winform用AForge拍照功能

2021-03-26 Windows程序

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);
//修改以后 删除本地上张图片....

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/68124.html