当前位置: 首页 > 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.dtcms.com/a/272817.html

相关文章:

  • Kettle导入Excel文件进数据库时,数值发生错误的一种原因
  • 计算机视觉速成 之 概述
  • Ubuntu如何快速搭建docker以及使用代理访问
  • Linux入门篇学习——Linux 工具之 make 工具和 makefile 文件
  • 数据结构 顺序表(1)
  • 等保-linux-三权分立账号设置,系统管理员、安全管理员、审计管理员
  • 目标检测中的评价指标计算
  • 数据结构 —— 键值对 map
  • Git操作技巧(一)
  • shell学习从入门到精通
  • 数据仓库:企业数据管理的核心枢纽
  • 创客匠人视角:从信任经济看创始人 IP 如何重塑 IP 变现逻辑
  • nmap扫描指定网段中指定开放端口的主机(IP,mac地址)
  • 量子计算系统软件:让“脆弱”的量子计算机真正可用
  • 有什么好使用的内网ip映射软件?可以让家庭/公司网络局域网端口,让公网互联网跨网访问
  • Spring boot整合dubbo+zookeeper
  • 26-计组-外存
  • 基于云端EC2的O3DE机器人仿真环境搭建指南
  • python自动化面试问题——关于python
  • 029_构造器重载与默认构造器
  • 【报错】Could not find a package configuration file provided by ‘glog’”
  • “Datawhale AI夏令营”基于带货视频评论的用户洞察挑战赛
  • 使用Tensorflow和CNN进行猫狗图片训练的实战总结
  • P1722 矩阵 II 题解 DFS深度优先遍历与卡特兰数(Catalan number)解
  • Spring Boot+Redis+Caffeine 二级缓存架构的终极实现方案、包含万级QPS下的黄金配置参数、全文超过2500字(博君一赞)
  • XGBoosting算法详解(Boosting思想的代表算法)
  • C语言<数据结构-链表>
  • LangChain RAG 实战
  • Transformers 和 PyTorch 的区别与安装指南
  • Docker 高级管理--Dockerfile镜像制作