C# 摄像头拍照完整实例

 /// <summary>
        /// 连接摄像头
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            CameraConn();
        }
 /// <summary>
        /// 拍照
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click_1(object sender, EventArgs e)
        {

            string RYID = txt_RYID.Text.ToString();
            if (RYID == null || RYID == "")
            {
                MessageBox.Show("请选择人员");
                return;
            }
            if (MessageBox.Show("确定是该人员:" + txt_XM.Text.ToString(), "提示", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                return;
            }
            if (videoSourcePlayer.IsRunning)
                {
                  string path = System.Environment.CurrentDirectory+"\\摄像头照片\\";
                  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));
                  picName = path + txt_XM.Text.ToString() +txt_RYID.Text.ToString()+ ".jpg";
                  picName2 = path + txt_XM.Text.ToString() + txt_RYID.Text.ToString() +"2.jpg";
                  if (!Directory.Exists(path))
                {
                      Directory.CreateDirectory(path);
                 }
                  if (File.Exists(picName))
                  {
                      File.Delete(picName);
                  }
                  if (File.Exists(picName2))
                  {
                      File.Delete(picName2);
                  }
                  using (Stream stream = File.Create(picName))
                  {
                      pE.Save(stream);
                  }
                  ///将图片左右反转
                  if (File.Exists(picName))
                  {
                      Bitmap bmp = new Bitmap(picName);
                      bmp= RevPic(bmp, 1280, 720);
                      bmp.Save(picName2, System.Drawing.Imaging.ImageFormat.Jpeg);
                  }
                    
                  MessageBox.Show("拍照成功");
                 
                }
        }
//关闭摄像头
private void button8_Click(object sender, EventArgs e)
        {
            videoSourcePlayer.SignalToStop();
            videoSourcePlayer.WaitForStop();
        }

 

posted @ 2018-05-14 12:32  Ever.心动  阅读(3644)  评论(0编辑  收藏  举报