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();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM