From ae0d9092bc828a5260c4f506c976c32b7bb2feab Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期六, 20 十二月 2025 14:11:59 +0800
Subject: [PATCH] 优化相机设置和异常处理逻辑

---
 LB_VisionProcesses/Cameras/HRCamera.cs     |  427 +++++++++++++++++++++++++++++++++++------------
 LB_VisionProcesses/Cameras/2DCameraForm.cs |  103 ++++++++++
 2 files changed, 413 insertions(+), 117 deletions(-)

diff --git a/LB_VisionProcesses/Cameras/2DCameraForm.cs b/LB_VisionProcesses/Cameras/2DCameraForm.cs
index 958dada..252b28f 100644
--- a/LB_VisionProcesses/Cameras/2DCameraForm.cs
+++ b/LB_VisionProcesses/Cameras/2DCameraForm.cs
@@ -37,9 +37,9 @@
         {
             InitializeComponent();
             //浼犲叆鐩告満鍙ユ焺鍚庣鐢ㄨ繛鎺�/鏂紑鎸夐敭
-            this.btnOpen.Enabled = false;
-            this.btnClose.Enabled = false;
-            this.cmbBrand.Enabled = false;
+            //this.btnOpen.Enabled = false;
+            //this.btnClose.Enabled = false;
+            //this.cmbBrand.Enabled = false;
             this.panel_Picture.Controls.Clear();
 
             this.dicCameras = dicCameras;
@@ -203,6 +203,7 @@
                 int Index = cmbBrand.FindString(camera.Brand.ToString()); ;
                 cmbBrand.Text = camera.Brand.ToString();
                 cmbBrand.SelectedIndex = Index;
+                this.btnEdit.Enabled = true;
             }
         }
 
@@ -236,6 +237,7 @@
                     camera.ImageGrabbed -= GetImageBllComplete;
                     camera.ImageGrabbed += GetImageBllComplete;
                     MessageBox.Show(camera.SN + "鎵撳紑鎴愬姛");
+                    this.btnEdit.Enabled = true;
                 }
             }
             else
@@ -282,6 +284,67 @@
         {
             if (camera == null)
                 return;
+
+            using (Form editForm = new Form())
+            {
+                editForm.Text = "楂樼骇鍙傛暟璁剧疆 - " + camera.SN;
+                editForm.Size = new System.Drawing.Size(400, 500);
+                editForm.StartPosition = FormStartPosition.CenterParent;
+                editForm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
+
+                PropertyGrid pg = new PropertyGrid();
+                pg.Dock = DockStyle.Fill;
+                pg.SelectedObject = new CameraAdvancedSettings(camera);
+
+                editForm.Controls.Add(pg);
+                editForm.ShowDialog();
+            }
+        }
+
+        /// <summary>
+        /// 鐩告満楂樼骇璁剧疆鍖呰绫�
+        /// </summary>
+        public class CameraAdvancedSettings
+        {
+            private BaseCamera _camera;
+            public CameraAdvancedSettings(BaseCamera camera)
+            {
+                _camera = camera;
+            }
+
+            [System.ComponentModel.Category("瑙﹀彂璁剧疆"), System.ComponentModel.Description("瑙﹀彂婧�")]
+            public TriggerSource TriggerSource
+            {
+                get { _camera.GetTriggerMode(out _, out TriggerSource s); return s; }
+                set { _camera.GetTriggerMode(out TriggerMode m, out _); _camera.SetTriggerMode(m, value); }
+            }
+
+            [System.ComponentModel.Category("瑙﹀彂璁剧疆"), System.ComponentModel.Description("瑙﹀彂鏋佹��")]
+            public TriggerPolarity TriggerPolarity
+            {
+                get { _camera.GetTriggerPolarity(out TriggerPolarity p); return p; }
+                set { _camera.SetTriggerPolarity(value); }
+            }
+
+            [System.ComponentModel.Category("瑙﹀彂璁剧疆"), System.ComponentModel.Description("瑙﹀彂寤舵椂(us)")]
+            public double TriggerDelay
+            {
+                get { _camera.GetTriggerDelay(out double d); return d; }
+                set { _camera.SetTriggerDelay(value); }
+            }
+
+            [System.ComponentModel.Category("瑙﹀彂璁剧疆"), System.ComponentModel.Description("瑙﹀彂婊ゆ尝(us)")]
+            public double TriggerFilter
+            {
+                get { _camera.GetTriggerFliter(out double f); return f; }
+                set { _camera.SetTriggerFliter(value); }
+            }
+
+            [System.ComponentModel.Category("鍥惧儚璁剧疆"), System.ComponentModel.Description("鑷姩鐧藉钩琛�")]
+            public void AutoBalanceWhite()
+            {
+                _camera.AutoBalanceWhite();
+            }
         }
 
         /// <summary>
@@ -516,9 +579,25 @@
 
             if (res == DialogResult.Yes)  //淇濆瓨VPP
             {
+                TriggerSource actualSource = TriggerSource.Line0;
+                TriggerPolarity polarity = TriggerPolarity.RisingEdge;
+                double delay = 0;
+                double filter = 0;
+
+                if (camera != null)
+                {
+                    camera.GetTriggerMode(out _, out actualSource);
+                    camera.GetTriggerPolarity(out polarity);
+                    camera.GetTriggerDelay(out delay);
+                    camera.GetTriggerFliter(out filter);
+                }
+
                 camConfig.Params.Inputs.Add("鐩告満SN", cmbSN.Text);
                 camConfig.Params.Inputs.Add("瑙﹀彂妯″紡", TriggerMode.On);
-                camConfig.Params.Inputs.Add("瑙﹀彂鏂瑰紡", radioButtonSoft.Checked ? TriggerSource.Software : TriggerSource.Line0);
+                camConfig.Params.Inputs.Add("瑙﹀彂鏂瑰紡", radioButtonSoft.Checked ? TriggerSource.Software : actualSource);
+                camConfig.Params.Inputs.Add("瑙﹀彂鏋佹��", polarity);
+                camConfig.Params.Inputs.Add("瑙﹀彂寤舵椂", delay);
+                camConfig.Params.Inputs.Add("瑙﹀彂娑堟姈", filter);
                 camConfig.Params.Inputs.Add("鏄惁鏈湴鍙栧浘", ckbLocalTest.Checked);
                 camConfig.Params.Inputs.Add("鏈湴鍙栧浘璺緞", cmbImagesPath.Items.Cast<string>().ToList());
                 camConfig.Params.Inputs.Add("鏄惁澶辫触閲嶆柊鍙栧浘", ckbRegrab.Checked);
@@ -547,10 +626,11 @@
                 camera.ImageGrabbed -= GetImageBllComplete;
                 camera.StopGrabbing();
 
+                camera.GetTriggerMode(out _, out TriggerSource actualSource);
                 if (radioButtonSoft.Checked)
                     camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
                 else
-                    camera.SetTriggerMode(TriggerMode.On, TriggerSource.Line0);
+                    camera.SetTriggerMode(TriggerMode.On, actualSource);
 
                 camera.StartGrabbing();
             }
@@ -620,10 +700,21 @@
 
             if (camera == null)
                 return;
+
+            camera.GetTriggerMode(out _, out TriggerSource currentSource);
+
             if (radioButtonSoft.Checked)
+            {
                 camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
+            }
             else
-                camera.SetTriggerMode(TriggerMode.On, TriggerSource.Line0);
+            {
+                // 濡傛灉褰撳墠宸茬粡鏄‖浠惰Е鍙戞簮锛屽垯淇濇寔锛涘惁鍒欓粯璁ine0
+                if (currentSource == TriggerSource.Software)
+                    camera.SetTriggerMode(TriggerMode.On, TriggerSource.Line0);
+                else
+                    camera.SetTriggerMode(TriggerMode.On, currentSource);
+            }
         }
 
         System.Windows.Forms.ToolTip ToolTip = new System.Windows.Forms.ToolTip();
diff --git a/LB_VisionProcesses/Cameras/HRCamera.cs b/LB_VisionProcesses/Cameras/HRCamera.cs
index 4695dcd..8c12123 100644
--- a/LB_VisionProcesses/Cameras/HRCamera.cs
+++ b/LB_VisionProcesses/Cameras/HRCamera.cs
@@ -1,6 +1,7 @@
 锘縰sing MVSDK_Net;
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
 using System.Drawing;
 using System.Drawing.Imaging;
 using System.Linq;
@@ -22,6 +23,7 @@
         private Thread _grabThread; // 鍥惧儚閲囬泦绾跨▼
         private bool _isGrabbing; // 閲囬泦鐘舵�佹爣蹇�
         private bool _threadRunning; // 绾跨▼杩愯鏍囧織
+        private bool _handleCreated = false; // 鍙ユ焺鏄惁宸插垱寤�
         private Thread _callbackThread; // 鍥炶皟澶勭悊绾跨▼
         private List<IMVDefine.IMV_Frame> _frameList; // 鍥惧儚缂撳瓨鍒楄〃
         private readonly object _frameLock = new object(); // 甯х紦瀛橀攣
@@ -90,7 +92,8 @@
                 else
                 {
                     // 璁板綍鏃ュ織鎴栨姏鍑哄紓甯�
-                    throw new Exception($"鏋氫妇璁惧澶辫触锛岄敊璇爜锛歿result}");
+                    //throw new Exception($"鏋氫妇璁惧澶辫触锛岄敊璇爜锛歿result}");
+                    Debug.WriteLine("鏋氫妇璁惧澶辫触锛�");
                 }
             }
             catch (Exception ex)
@@ -113,10 +116,19 @@
         {
             try
             {
-                // 濡傛灉鐩告満宸叉墦寮�锛屽厛鍏抽棴
-                if (_camera != null && _camera.IMV_IsOpen())
+                // 纭繚褰诲簳鍏抽棴鍜屾竻鐞�
+                if (_camera != null)
                 {
-                    CloseDevice();
+                    if (_camera.IMV_IsOpen())
+                    {
+                        _camera.IMV_Close();
+                    }
+                    
+                    if (_handleCreated)
+                    {
+                        _camera.IMV_DestroyHandle();
+                        _handleCreated = false;
+                    }
                 }
                 
                 // 鏋氫妇璁惧骞跺尮閰峉N
@@ -143,6 +155,7 @@
                 {
                     throw new Exception($"鍒涘缓璁惧鍙ユ焺澶辫触锛岄敊璇爜锛歿result}");
                 }
+                _handleCreated = true;
                 
                 // 鎵撳紑璁惧
                 result = _camera.IMV_Open();
@@ -152,7 +165,7 @@
                 }
                 
                 // 璁剧疆璁惧灞炴��
-                SN = SN;
+                this.SN = SN;
                 isGrabbing = false;
                 
                 // 璁剧疆缂撳瓨涓暟涓�8
@@ -192,6 +205,13 @@
                     {
                         System.Diagnostics.Debug.WriteLine($"鍏抽棴鐩告満澶辫触锛岄敊璇爜锛歿result}");
                     }
+                }
+
+                // 閿�姣佸彞鏌�
+                if (_handleCreated)
+                {
+                    _camera.IMV_DestroyHandle();
+                    _handleCreated = false;
                 }
                 
                 // 閲婃斁璧勬簮
@@ -519,27 +539,27 @@
                     throw new Exception("鐩告満鏈墦寮�");
                 }
                 
+                string gainFeature = _camera.IMV_FeatureIsValid("Gain") ? "Gain" : "GainRaw";
+                
                 // 楠岃瘉澧炵泭鑼冨洿
                 double minGain = 0, maxGain = 0;
-                int result = _camera.IMV_GetDoubleFeatureMin("GainRaw", ref minGain);
+                int result = _camera.IMV_GetDoubleFeatureMin(gainFeature, ref minGain);
                 if (result != IMVDefine.IMV_OK)
                 {
                     throw new Exception($"鑾峰彇澧炵泭鏈�灏忓�煎け璐ワ紝閿欒鐮侊細{result}");
                 }
                 
-                result = _camera.IMV_GetDoubleFeatureMax("GainRaw", ref maxGain);
+                result = _camera.IMV_GetDoubleFeatureMax(gainFeature, ref maxGain);
                 if (result != IMVDefine.IMV_OK)
                 {
                     throw new Exception($"鑾峰彇澧炵泭鏈�澶у�煎け璐ワ紝閿欒鐮侊細{result}");
                 }
                 
-                if (gain < minGain || gain > maxGain)
-                {
-                    throw new Exception($"澧炵泭鍊艰秴鍑鸿寖鍥达紝鏈夋晥鑼冨洿锛歿minGain} - {maxGain}");
-                }
+                if (gain < minGain) gain = minGain;
+                if (gain > maxGain) gain = maxGain;
                 
                 // 璁剧疆澧炵泭
-                result = _camera.IMV_SetDoubleFeatureValue("GainRaw", gain);
+                result = _camera.IMV_SetDoubleFeatureValue(gainFeature, gain);
                 if (result != IMVDefine.IMV_OK)
                 {
                     throw new Exception($"璁剧疆澧炵泭澶辫触锛岄敊璇爜锛歿result}");
@@ -570,7 +590,8 @@
                     throw new Exception("鐩告満鏈墦寮�");
                 }
                 
-                int result = _camera.IMV_GetDoubleFeatureValue("GainRaw", ref gain);
+                string gainFeature = _camera.IMV_FeatureIsValid("Gain") ? "Gain" : "GainRaw";
+                int result = _camera.IMV_GetDoubleFeatureValue(gainFeature, ref gain);
                 return result == IMVDefine.IMV_OK;
             }
             catch (Exception ex)
@@ -580,32 +601,278 @@
             }
         }
         
-        // 鍏朵粬鍙傛暟璁剧疆鏂规硶绫讳技锛岃繖閲岀渷鐣ラ儴鍒嗗疄鐜�...
-        
+        /// <summary>
+        /// 璁剧疆瑙﹀彂鏋佹��
+        /// </summary>
+        /// <param name="polarity">瑙﹀彂鏋佹��</param>
+        /// <returns>鏄惁鎴愬姛</returns>
+        public override bool SetTriggerPolarity(TriggerPolarity polarity)
+        {
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                string activation = (polarity == TriggerPolarity.RisingEdge || polarity == TriggerPolarity.HighLevel) 
+                    ? "RisingEdge" : "FallingEdge";
+                
+                int result = _camera.IMV_SetEnumFeatureSymbol("TriggerActivation", activation);
+                return result == IMVDefine.IMV_OK;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"璁剧疆瑙﹀彂鏋佹�уけ璐ワ細{ex.Message}");
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 鑾峰彇瑙﹀彂鏋佹��
+        /// </summary>
+        /// <param name="polarity">瑙﹀彂鏋佹��</param>
+        /// <returns>鏄惁鎴愬姛</returns>
+        public override bool GetTriggerPolarity(out TriggerPolarity polarity)
+        {
+            polarity = TriggerPolarity.RisingEdge;
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                IMVDefine.IMV_String activation = new IMVDefine.IMV_String();
+                int result = _camera.IMV_GetEnumFeatureSymbol("TriggerActivation", ref activation);
+                if (result == IMVDefine.IMV_OK)
+                {
+                    polarity = activation.str == "RisingEdge" ? TriggerPolarity.RisingEdge : TriggerPolarity.FallingEdge;
+                    return true;
+                }
+                return false;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"鑾峰彇瑙﹀彂鏋佹�уけ璐ワ細{ex.Message}");
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 璁剧疆瑙﹀彂婊ゆ尝鏃堕棿 (us)
+        /// </summary>
+        public override bool SetTriggerFliter(double flitertime)
+        {
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                // 鍗庣澘鐩告満閫氬父浣跨敤 LineDebouncerTime 鎺у埗婊ゆ尝
+                if (_camera.IMV_FeatureIsValid("LineDebouncerTime"))
+                {
+                    int result = _camera.IMV_SetDoubleFeatureValue("LineDebouncerTime", flitertime);
+                    return result == IMVDefine.IMV_OK;
+                }
+                return false;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"璁剧疆瑙﹀彂婊ゆ尝澶辫触锛歿ex.Message}");
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 鑾峰彇瑙﹀彂婊ゆ尝鏃堕棿 (us)
+        /// </summary>
+        public override bool GetTriggerFliter(out double flitertime)
+        {
+            flitertime = 0;
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                if (_camera.IMV_FeatureIsValid("LineDebouncerTime"))
+                {
+                    int result = _camera.IMV_GetDoubleFeatureValue("LineDebouncerTime", ref flitertime);
+                    return result == IMVDefine.IMV_OK;
+                }
+                return false;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"鑾峰彇瑙﹀彂婊ゆ尝澶辫触锛歿ex.Message}");
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 璁剧疆瑙﹀彂寤舵椂 (us)
+        /// </summary>
+        public override bool SetTriggerDelay(double delay)
+        {
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                if (_camera.IMV_FeatureIsValid("TriggerDelay"))
+                {
+                    int result = _camera.IMV_SetDoubleFeatureValue("TriggerDelay", delay);
+                    return result == IMVDefine.IMV_OK;
+                }
+                return false;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"璁剧疆瑙﹀彂寤舵椂澶辫触锛歿ex.Message}");
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 鑾峰彇瑙﹀彂寤舵椂 (us)
+        /// </summary>
+        public override bool GetTriggerDelay(out double delay)
+        {
+            delay = 0;
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                if (_camera.IMV_FeatureIsValid("TriggerDelay"))
+                {
+                    int result = _camera.IMV_GetDoubleFeatureValue("TriggerDelay", ref delay);
+                    return result == IMVDefine.IMV_OK;
+                }
+                return false;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"鑾峰彇瑙﹀彂寤舵椂澶辫触锛歿ex.Message}");
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 璁剧疆淇″彿绾挎ā寮�
+        /// </summary>
+        public override bool SetLineMode(IOLines line, LineMode mode)
+        {
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                // 閫夋嫨绾胯矾
+                int result = _camera.IMV_SetEnumFeatureSymbol("LineSelector", line.ToString());
+                if (result != IMVDefine.IMV_OK) return false;
+                
+                // 璁剧疆妯″紡
+                string lineMode = mode == LineMode.Input ? "Input" : "Output";
+                result = _camera.IMV_SetEnumFeatureSymbol("LineMode", lineMode);
+                return result == IMVDefine.IMV_OK;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"璁剧疆淇″彿绾挎ā寮忓け璐ワ細{ex.Message}");
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 璁剧疆淇″彿绾跨數骞崇姸鎬�
+        /// </summary>
+        public override bool SetLineStatus(IOLines line, LineStatus linestatus)
+        {
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                // 浠呭杈撳嚭绾胯矾鏈夋晥
+                int result = _camera.IMV_SetEnumFeatureSymbol("LineSelector", line.ToString());
+                if (result != IMVDefine.IMV_OK) return false;
+                
+                bool status = linestatus == LineStatus.Hight;
+                result = _camera.IMV_SetBoolFeatureValue("UserOutputValue", status);
+                return result == IMVDefine.IMV_OK;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"璁剧疆淇″彿绾跨姸鎬佸け璐ワ細{ex.Message}");
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 鑾峰彇淇″彿绾跨數骞崇姸鎬�
+        /// </summary>
+        public override bool GetLineStatus(IOLines line, out LineStatus lineStatus)
+        {
+            lineStatus = LineStatus.Low;
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                int result = _camera.IMV_SetEnumFeatureSymbol("LineSelector", line.ToString());
+                if (result != IMVDefine.IMV_OK) return false;
+                
+                bool status = false;
+                result = _camera.IMV_GetBoolFeatureValue("LineStatus", ref status);
+                if (result == IMVDefine.IMV_OK)
+                {
+                    lineStatus = status ? LineStatus.Hight : LineStatus.Low;
+                    return true;
+                }
+                return false;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"鑾峰彇淇″彿绾跨姸鎬佸け璐ワ細{ex.Message}");
+                return false;
+            }
+        }
+
+        /// <summary>
+        /// 鑷姩鐧藉钩琛�
+        /// </summary>
+        public override bool AutoBalanceWhite()
+        {
+            try
+            {
+                if (_camera == null || !_camera.IMV_IsOpen()) return false;
+                
+                if (_camera.IMV_FeatureIsValid("BalanceWhiteAuto"))
+                {
+                    int result = _camera.IMV_SetEnumFeatureSymbol("BalanceWhiteAuto", "Once");
+                    return result == IMVDefine.IMV_OK;
+                }
+                return false;
+            }
+            catch (Exception ex)
+            {
+                System.Diagnostics.Debug.WriteLine($"鑷姩鐧藉钩琛″け璐ワ細{ex.Message}");
+                return false;
+            }
+        }
+
         #endregion
 
-        #region 杈呭姪鏂规硶
-        
+        #region 閲囬泦鍜岃浆鎹㈣緟鍔╂柟娉�
+
         /// <summary>
         /// 鍥惧儚閲囬泦绾跨▼澶勭悊鍑芥暟
         /// </summary>
         private void GrabThreadProc()
         {
             IMVDefine.IMV_Frame frame = new IMVDefine.IMV_Frame();
-            
+
             while (_threadRunning)
             {
                 try
                 {
                     // 鑾峰彇鍥惧儚甯�
                     int result = _camera.IMV_GetFrame(ref frame, 100);
-                    
+
                     if (result == IMVDefine.IMV_OK)
                     {
                         // 澶勭悊鍥惧儚甯�
                         ProcessFrame(frame);
                     }
-                    else if (result != 0x80000001) // 瓒呮椂閿欒浠g爜锛堝父瑙佸�硷級
+                    else if (result != 0x80000001) // 瓒呮椂閿欒浠g爜
                     {
                         // 闈炶秴鏃堕敊璇�
                         System.Diagnostics.Debug.WriteLine($"鑾峰彇鍥惧儚甯уけ璐ワ紝閿欒鐮侊細{result}");
@@ -615,11 +882,11 @@
                 {
                     System.Diagnostics.Debug.WriteLine($"閲囬泦绾跨▼寮傚父锛歿ex.Message}");
                 }
-                
+
                 Thread.Sleep(1);
             }
         }
-        
+
         /// <summary>
         /// 澶勭悊鍥惧儚甯�
         /// </summary>
@@ -630,26 +897,27 @@
             {
                 // 灏嗗浘鍍忔暟鎹浆鎹负Bitmap
                 Bitmap bitmap = ConvertFrameToBitmap(frame);
-                
+
                 if (bitmap != null)
                 {
                     // 瑙﹀彂鍥惧儚閲囬泦浜嬩欢
                     CameraEventArgs args = new CameraEventArgs(SN, bitmap);
                     ImageGrabbed?.Invoke(this, args);
-                    
+
                     // 鏇存柊鍥炶皟鍥惧儚
                     CallBackImg = bitmap;
                 }
-                
+
                 // 閲婃斁甯�
-                _camera.IMV_ReleaseFrame(ref frame);
+                var tempFrame = frame;
+                _camera.IMV_ReleaseFrame(ref tempFrame);
             }
             catch (Exception ex)
             {
                 System.Diagnostics.Debug.WriteLine($"澶勭悊鍥惧儚甯уけ璐ワ細{ex.Message}");
             }
         }
-        
+
         /// <summary>
         /// 灏嗗浘鍍忓抚杞崲涓築itmap
         /// </summary>
@@ -660,7 +928,7 @@
             try
             {
                 Bitmap bitmap = null;
-                
+
                 switch (frame.frameInfo.pixelFormat)
                 {
                     case IMVDefine.IMV_EPixelType.gvspPixelMono8:
@@ -674,7 +942,7 @@
                         bitmap = ConvertToBGR8(frame);
                         break;
                 }
-                
+
                 return bitmap;
             }
             catch (Exception ex)
@@ -683,14 +951,14 @@
                 return null;
             }
         }
-        
+
         /// <summary>
         /// 鍒涘缓Mono8鏍煎紡Bitmap
         /// </summary>
         private Bitmap CreateMono8Bitmap(IMVDefine.IMV_Frame frame)
         {
             Bitmap bitmap = new Bitmap((int)frame.frameInfo.width, (int)frame.frameInfo.height, PixelFormat.Format8bppIndexed);
-            
+
             // 璁剧疆鐏板害璋冭壊鏉�
             ColorPalette palette = bitmap.Palette;
             for (int i = 0; i < 256; i++)
@@ -698,39 +966,39 @@
                 palette.Entries[i] = Color.FromArgb(i, i, i);
             }
             bitmap.Palette = palette;
-            
+
             // 澶嶅埗鍥惧儚鏁版嵁
             BitmapData bmpData = bitmap.LockBits(
                 new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                 ImageLockMode.WriteOnly,
                 bitmap.PixelFormat);
-            
+
             // 浣跨敤CopyMemory API杩涜鍐呭瓨澶嶅埗
             CopyMemory(bmpData.Scan0, frame.pData, (uint)Math.Min(frame.frameInfo.size, (uint)(bmpData.Stride * bitmap.Height)));
-            
+
             bitmap.UnlockBits(bmpData);
             return bitmap;
         }
-        
+
         /// <summary>
         /// 鍒涘缓BGR8鏍煎紡Bitmap
         /// </summary>
         private Bitmap CreateBgr8Bitmap(IMVDefine.IMV_Frame frame)
         {
             Bitmap bitmap = new Bitmap((int)frame.frameInfo.width, (int)frame.frameInfo.height, PixelFormat.Format24bppRgb);
-            
+
             BitmapData bmpData = bitmap.LockBits(
                 new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                 ImageLockMode.WriteOnly,
                 bitmap.PixelFormat);
-            
+
             // 浣跨敤CopyMemory API杩涜鍐呭瓨澶嶅埗
             CopyMemory(bmpData.Scan0, frame.pData, (uint)Math.Min(frame.frameInfo.size, (uint)(bmpData.Stride * bitmap.Height)));
-            
+
             bitmap.UnlockBits(bmpData);
             return bitmap;
         }
-        
+
         /// <summary>
         /// 杞崲涓築GR8鏍煎紡
         /// </summary>
@@ -749,10 +1017,10 @@
                 eDstPixelFormat = IMVDefine.IMV_EPixelType.gvspPixelBGR8,
                 nDstBufSize = frame.frameInfo.width * frame.frameInfo.height * 3
             };
-            
+
             IntPtr dstBuffer = Marshal.AllocHGlobal((int)convertParam.nDstBufSize);
             convertParam.pDstBuf = dstBuffer;
-            
+
             try
             {
                 int result = _camera.IMV_PixelConvert(ref convertParam);
@@ -767,26 +1035,26 @@
                 Marshal.FreeHGlobal(dstBuffer);
             }
         }
-        
+
         /// <summary>
         /// 浠庣紦鍐插尯鍒涘缓BGR8 Bitmap
         /// </summary>
         private Bitmap CreateBgr8BitmapFromBuffer(IntPtr buffer, uint width, uint height)
         {
             Bitmap bitmap = new Bitmap((int)width, (int)height, PixelFormat.Format24bppRgb);
-            
+
             BitmapData bmpData = bitmap.LockBits(
                 new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                 ImageLockMode.WriteOnly,
                 bitmap.PixelFormat);
-            
+
             // 浣跨敤CopyMemory API杩涜瀹夊叏鐨勫唴瀛樺鍒�
             CopyMemory(bmpData.Scan0, buffer, (uint)(width * height * 3));
-            
+
             bitmap.UnlockBits(bmpData);
             return bitmap;
         }
-        
+
         /// <summary>
         /// 灏嗚Е鍙戞簮鏋氫妇杞崲涓哄瓧绗︿覆
         /// </summary>
@@ -804,7 +1072,7 @@
                 _ => "Software"
             };
         }
-        
+
         /// <summary>
         /// 灏嗗瓧绗︿覆杞崲涓鸿Е鍙戞簮鏋氫妇
         /// </summary>
@@ -822,72 +1090,9 @@
                 _ => TriggerSource.Software
             };
         }
-        
+
         #endregion
 
-        #region 鏈疄鐜扮殑鎶借薄鏂规硶锛堢畝鍖栫増瀹炵幇锛�
-        
-        public override bool SetTriggerPolarity(TriggerPolarity polarity)
-        {
-            // 鍗庣澘鐩告満鍙兘涓嶆敮鎸佽鍔熻兘锛岃繑鍥炴垚鍔�
-            return true;
-        }
-        
-        public override bool GetTriggerPolarity(out TriggerPolarity polarity)
-        {
-            polarity = TriggerPolarity.RisingEdge;
-            return true;
-        }
-        
-        public override bool SetTriggerFliter(double flitertime)
-        {
-            // 鏆備笉鏀寔
-            return true;
-        }
-        
-        public override bool GetTriggerFliter(out double flitertime)
-        {
-            flitertime = 0;
-            return true;
-        }
-        
-        public override bool SetTriggerDelay(double delay)
-        {
-            // 鏆備笉鏀寔
-            return true;
-        }
-        
-        public override bool GetTriggerDelay(out double delay)
-        {
-            delay = 0;
-            return true;
-        }
-        
-        public override bool SetLineMode(IOLines line, LineMode mode)
-        {
-            // 鏆備笉鏀寔
-            return true;
-        }
-        
-        public override bool SetLineStatus(IOLines line, LineStatus linestatus)
-        {
-            // 鏆備笉鏀寔
-            return true;
-        }
-        
-        public override bool GetLineStatus(IOLines line, out LineStatus lineStatus)
-        {
-            lineStatus = LineStatus.Low;
-            return true;
-        }
-        
-        public override bool AutoBalanceWhite()
-        {
-            // 鏆備笉鏀寔鑷姩鐧藉钩琛�
-            return true;
-        }
-        
-        #endregion
 
         #region IDisposable瀹炵幇
         

--
Gitblit v1.9.3