当前位置: 首页 > news >正文

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

代码实现调用摄像头拍照
初始化摄像头:

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

实现拍照显示并且保存

 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("拍照有误!", "提示");}
http://www.lryc.cn/news/583935.html

相关文章:

  • JS实现页面实时时间显示/倒计时
  • Java 大视界 -- Java 大数据在智能交通智能停车诱导与车位共享中的应用(341)
  • selenium跳转到新页面时如何进行定位
  • 编写bat文件自动打开chrome浏览器,并通过selenium抓取浏览器操作chrome
  • 如何使用Java WebSocket API实现客户端和服务器端的通信?
  • Language Models are Few-Shot Learners: 开箱即用的GPT-3(二)
  • Android 应用常见安全问题
  • Windows Edge 播放 H.265 视频指南
  • 多模态大语言模型arxiv论文略读(156)
  • 论文阅读笔记:VI-Net: Boosting Category-level 6D Object Pose Estimation
  • web前端面试笔记
  • 微软365 PDF导出功能存在本地文件包含漏洞,可泄露敏感服务器数据
  • C#集合:从基础到进阶的全面解析
  • qemu vcpu的创建过程
  • Debian-10编译安装Mysql-5.7.44 笔记250706
  • Windows 11 安装过程中跳过微软账户创建本地账户
  • 深度学习_全连接神经网络
  • 深入理解Java虚拟机:Java内存区域与内存溢出异常
  • Linux 操作系统如何实现软硬件解耦?从容器与硬件接口封装谈起
  • 数字孪生技术引领UI前端设计新趋势:增强现实与虚拟现实的融合应用
  • 计算机学科专业基础综合(408)四门核心课程的知识点总结
  • Docker高级管理--容器通信技术与数据持久化
  • 从零开始搭建深度学习大厦系列-3.卷积神经网络基础(5-9)
  • 【网络编程】 TCP 协议栈的知识汇总
  • 【运维实战】解决 K8s 节点无法拉取 pause:3.6 镜像导致 API Server 启动失败的问题
  • Spring boot整合dubbo+zookeeper
  • IDEA 安装AI代码助手GitHub Copilot和简单使用体验
  • 【科研绘图系列】R语言探索生物多样性与地理分布的可视化之旅
  • Jekyll + Chirpy + GitHub Pages 搭建博客
  • 微服务架构的演进:迈向云原生——Java技术栈的实践之路