界面上有一个Emgucv 的imagebox 控件,用来显示摄像头的影像,另外还有三个Picturebox,这三个PictureBox的父控件都设置为imagebox,并且透明显示。代码如下:
picScan.Parent = imgGestureCapture; picScan.BackColor = Color.Transparent; picScanHand.Parent = picScan; picScanHand.BackColor = Color.Transparent; picScanHandPay.Parent = picScan; picScanHandPay.BackColor = Color.Transparent;
在image的ImageGrabbed事件中有检测人脸的代码:
private void GestureVideoGetMat() { try { Mat mat = videoGesturePay.GetMat(); //取出一帧 Image<Bgr, Byte> MatImg = mat.ToImage<Bgr, Byte>(); if (isDetectFace) { long now = Utility.GetTimeStamp_Millseconds(); System.Drawing.Rectangle[] rects = face_cascade.DetectMultiScale(MatImg, 1.1, 10, new Size(frmStart.FaceDetectSize, frmStart.FaceDetectSize), Size.Empty); if (rects.Length > 0) { //检测并记录人脸数量 videoGesturePay.detectFaceNum = rects.Length; videoGesturePay.detectFaceTime = now; } else { videoGesturePay.detectFaceNum = 0; videoGesturePay.noDetectFaceTime = now; } } imgGestureCapture.Image = MatImg; //显示图像 } catch(Exception ex) { StackTrace st = new StackTrace(new StackFrame(true)); LogHelper.WriteLog(LogLevel.Error, st,"GestureVideoGetMat:" + ex.Message); reLoadPic(); } }
现在的问题是不定期出现以下异常:
记录时间:2018-01-17 00:19:48,353 线程ID:[1] Application_ThreadException 出现应用程序未处理的异常:2018/1/17 0:19:48/r/n异常类型:ArgumentException/r/n异常消息:参数无效。/r/n异常信息: 在 System.Drawing.Image.get_Width()
在 System.Drawing.Image.get_Size()
在 System.Windows.Forms.PictureBox.ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
在 System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
在 Emgu.CV.UI.PanAndZoomPictureBox.OnPaint(PaintEventArgs pe) 位置 E:\Setup\emgucv-windesktop 3.3.0.2824\Emgu.CV.UI\PanAndZoomPictureBox.cs:行号 231
在 System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle, Region transparentRegion)
在 System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
在 System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
在 System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
在 System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle, Region transparentRegion)
在 System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
在 System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
在 System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
在 System.Windows.Forms.Control.WmPaint(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.Label.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)/r/n
然后界面的picturebox就会显示个大红叉:
请教如何解决这问题?