C#System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误。

代码实现调用摄像头拍照

初始化摄像头:

csharp 复制代码
   bool isConnectSuccess = false;
   private VideoCaptureDevice videoDevice;
   public FilterInfoCollection videoDevices;
   public VideoCaptureDevice videoSource;
     public void LoadCamera()
  {
      if (videoDevice != null && videoDevice.IsRunning)
      {
          videoDevice.SignalToStop();
          videoDevice.WaitForStop();

          if (videoDevice != null)
          {
              videoDevice.Stop();
          }
      }

      // FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
      // 获取可用的视频设备
      videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
      if (videoDevices.Count > 0)
      {
          MotionRealPlay.BackgroundImageLayout = ImageLayout.Stretch;

          if (videoDevices.Count >= 1)
          {
              //// 选择第CameraNumber个摄像头
              FilterInfo firstDevice = videoDevices[0];


              videoDevice = new VideoCaptureDevice(firstDevice.MonikerString);
              videoDevice.NewFrame += new NewFrameEventHandler(VideoDevice_NewFrame);
              videoDevice.Start();



              //方法二

              // 缩放控制条  
             // TrackBar1 = new TrackBar { Dock = DockStyle.Bottom, Minimum = 10, Maximum = 200, Value = 100 };
            //  TrackBar1.ValueChanged += (s, e) => zoomFactor_width = TrackBar1.Value;
            //  TrackBar2.ValueChanged += (s, e) => zoomFactor_height = TrackBar2.Value;
             // Controls.Add(TrackBar1);

              // 绑定帧事件  
              //videoSource.NewFrame += (s, e) =>
              //{
              //    // 应用缩放  
              //    var resized = ResizeImage(e.Frame, zoomFactor);
              //    MotionRealPlay.Invoke((Action)(() => MotionRealPlay.Image = resized));
              //};

              //videoSource.Start();
            //  FormClosing += (s, e) => videoSource.Stop();

              isConnectSuccess = true;
          }
          else
          {
              //// 选择第一个摄像头
             
              MessageBox.Show("未找到摄像头设备!", "提示");
          }



      }
      else
      {
         
          MessageBox.Show("未找到摄像头设备!", "提示");
      }
  }
 string filePath = "";

 Bitmap image = null;
 private void VideoDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
 {


    


     // 在这里处理摄像头的视频流数据,例如显示在PictureBox控件中
     image = (Bitmap)eventArgs.Frame.Clone();

     if (this.IsHandleCreated)
     {
         this.BeginInvoke(new EventHandler(delegate
         {

             if (MotionRealPlay.Visible == true)
             {
                  MotionRealPlay.BackgroundImage = image;
                // txt_phone.Text = "width:" + zoomFactor_width;
                // txt_attend.Text = "height:" + zoomFactor_height;

                 //if (zoomFactor_width > 1)
                 //{
                 //    var resized = ResizeImage((Bitmap)eventArgs.Frame.Clone(), zoomFactor_width, zoomFactor_height);
                 //    MotionRealPlay.BeginInvoke((Action)(() => MotionRealPlay.Image = resized));
                 //}
               
                 // 根据缩放因子生成缩放后的图像
                 //using (Bitmap scaledImage = ResizeImage((Bitmap)eventArgs.Frame.Clone(), zoomFactor))
                 //{
                 //    MotionRealPlay.BackgroundImage = image;
                 //    //picBoxCamera.Image?.Dispose(); // 释放旧图像资源
                 //    //picBoxCamera.Image = scaledImage;
                 //    //videoDevice.
                 //}
             }


         }));

     }
     // 延迟1秒
     Thread.Sleep(10);


 }

实现拍照显示并且保存

csharp 复制代码
 try
 {
     if ( string.IsNullOrEmpty(Txt_Name.Text))
     {
         MessageBox.Show("请先输入人员姓名再拍照!", "提示");
         return;
     }


     if (isConnectSuccess == false)
     {
        // AddTextToRichTextBox1("未连接摄像头设备!", Color.Red);
         MessageBox.Show("未连接摄像头设备!", "提示");
     }

     if (this.IsHandleCreated)
     {
         this.BeginInvoke(new EventHandler(delegate
         {

             if (image != null)
             {


                 try
                 {
                    // Bitmap bitmap1 = image;

                    // Bitmap bm = new Bitmap(image, 400, 600);
                    // Bitmap bm = new Bitmap(image);
                     // 指定照片的保存路径
                     //string fileName = "Capture_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";
                     string fileName = Txt_Name.Text + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

                     filePath = @"Images\\" + fileName;

                     // 保存照片
                     //image.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                     //image.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                   

                    // PictureBox_PeoplePhoto.BackgroundImage = System.Drawing.Image.FromFile(filePath);

                     Txt_Img.Text = fileName;


                     //  bm.Dispose();
                     //  image.Dispose();

                     //  Bitmap bm = (Bitmap)eventArgs.Frame.Clone();//640*480
                     //方法二
                     // Bitmap bitmap1 = image.Clone(new Rectangle(((image.Width - image.Height) / 2) + 20, 10, image.Height - 20, image.Height - 20), PixelFormat.DontCare);//裁切图片
                     Bitmap bitmap1 = new Bitmap(image, 400, 600);
                     PictureBox_PeoplePhoto.Image = bitmap1;
                     bitmap1.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                 }
                 catch (Exception ex)
                 {
                     AddTextToRichTextBox1("拍照有误:" + ex.ToString(), Color.Red);
                     MessageBox.Show("拍照有误!", "提示");
                     return;
                 }
             }




         }));

     }
 }
 catch (Exception ex)
 {
     WriteTxt("拍照有误:" + ex.ToString());
     MessageBox.Show("拍照有误!", "提示");
 }
相关推荐
玩泥巴的6 小时前
存储那么贵,何不白嫖飞书云文件空间
c#·.net·二次开发·飞书
脑电信号要分类17 小时前
将多张图片拼接成一个pdf文件输出
pdf·c#·apache
njsgcs17 小时前
c# solidworks 折弯系数检查
开发语言·c#
格林威18 小时前
工业相机图像采集:Grab Timeout 设置建议——拒绝“假死”与“丢帧”的黄金法则
开发语言·人工智能·数码相机·计算机视觉·c#·机器视觉·工业相机
唐青枫19 小时前
C#.NET SignalR + Redis Backplane 深入解析:多节点部署与跨实例消息同步
c#·.net
FL16238631291 天前
[C#][winform]segment-anything分割万物部署onnx模型一键抠图演示
开发语言·c#
love530love1 天前
OpenClaw 手机直连配置全流程
人工智能·windows·python·智能手机·c#·agent·openclaw
bcbobo21cn1 天前
C# byte类型和byte数组的使用
开发语言·c#·字节数组·byte类型
月巴月巴白勺合鸟月半2 天前
一次PDF文件的处理(一)
pdf·c#
大鹏说大话2 天前
Java 锁膨胀机制深度解析:从偏向锁到重量级锁的进化之路
开发语言·c#