From fec341de45f4b3fd1825807f0b3261143fa13caa Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期一, 29 十二月 2025 17:03:45 +0800
Subject: [PATCH] 修复部分显示工具为面向对象。

---
 LB_SmartVision/ProcessRun/ProcessContext.cs |  109 +++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 79 insertions(+), 30 deletions(-)

diff --git a/LB_SmartVision/ProcessRun/ProcessContext.cs b/LB_SmartVision/ProcessRun/ProcessContext.cs
index 6f32ad5..79d4ada 100644
--- a/LB_SmartVision/ProcessRun/ProcessContext.cs
+++ b/LB_SmartVision/ProcessRun/ProcessContext.cs
@@ -301,7 +301,7 @@
             return res;
         }
 
-        public bool GetImage(Layout layout, out HObject InputImage, out HObject RecordImage)
+        public bool GetImage(Layout layout, out HImage InputImage, out HImage RecordImage)
         {
             InputImage = null; RecordImage = null;
             try
@@ -329,24 +329,37 @@
                     IndexValueName = arrOutputs[2];
 
                     object o_InputImage = ((IProcess)dicContext[IndexProcessName]).OutputImage;
-                    if (o_InputImage is HObject ho_image && ho_image.IsInitialized())
-                        InputImage = ho_image;
-                    else if (o_InputImage is Bitmap)
+                    if (o_InputImage != null && o_InputImage is HImage ho_image && ho_image.IsInitialized())
                     {
-                        //灏哅at杞崲涓篐Object
-                        TAlgorithm.Bitmap2HObject((Bitmap)o_InputImage, out HObject ho_RecordImage);
-                        InputImage = ho_RecordImage;
+                        InputImage = ho_image.Clone();
+                        ho_image.Dispose();
                     }
-                    else if (o_InputImage is Mat)
+                    else if (o_InputImage != null && o_InputImage is Bitmap)
                     {
                         //灏哅at杞崲涓篐Object
-                        TAlgorithm.Mat2HObject((Mat)o_InputImage, out HObject ho_RecordImage);
-                        InputImage = ho_RecordImage;
+                        using (HImage ho_RecordImage = TAlgorithm.Bitmap2HImage((Bitmap)o_InputImage))
+                        {
+                            if (ho_RecordImage != null && ho_RecordImage.Key != IntPtr.Zero)
+                            {
+                                InputImage = ho_RecordImage.Clone();
+                            }
+                        }
+                    }
+                    else if (o_InputImage != null && o_InputImage is Mat)
+                    {
+                        //灏哅at杞崲涓篐Object
+                        using (HImage ho_RecordImage = TAlgorithm.Mat2HImage((Mat)o_InputImage))
+                        {
+                            if (ho_RecordImage != null && ho_RecordImage.Key != IntPtr.Zero)
+                            {
+                                InputImage = ho_RecordImage.Clone();
+                            }
+                        }
                     }
 
-                    if (InputImage != null && InputImage.IsInitialized())
+                    if (InputImage != null && InputImage.Key != IntPtr.Zero && InputImage.IsInitialized())
                     {
-                        HOperatorSet.GetImageSize(InputImage, out ho_ImageWidth, out ho_ImageHeight);
+                        InputImage.GetImageSize(out ho_ImageWidth, out ho_ImageHeight);
 
                         //鍥剧墖灏哄鍙樺寲鎵嶆洿鏂扮獥鍙e昂瀵竅鎻愰珮閫熷害]
                         if ((ho_ImageWidth.Length > 0 && ho_ImageWidth.TupleInt() != hWindowControl.Size.Width)
@@ -374,11 +387,13 @@
 
                     if (dicContext.ContainsKey(IndexProcessName))
                     {
-                        ObjectRecord objectRecord1 = ((IProcess)dicContext[IndexProcessName]).Record;
-                        if (objectRecord1 != null)
+                        using (ObjectRecord objectRecord1 = ((IProcess)dicContext[IndexProcessName]).Record)
                         {
-                            objectRecord1.Display(hWindowControl.HalconWindow);
-                            objectRecord1.Dispose();
+                            if (objectRecord1 != null)
+                            {
+                                objectRecord1.Display(hWindowControl.HalconWindow);
+                                objectRecord1.Dispose();
+                            }
                         }
                     }
                 }
@@ -392,11 +407,13 @@
 
                     if (dicContext.ContainsKey(IndexProcessName))
                     {
-                        ObjectRecord objectRecord2 = ((IProcess)dicContext[IndexProcessName]).Record;
-                        if (objectRecord2 != null)
+                        using (ObjectRecord objectRecord2 = ((IProcess)dicContext[IndexProcessName]).Record)
                         {
-                            objectRecord2.Display(hWindowControl.HalconWindow);
-                            objectRecord2.Dispose();
+                            if (objectRecord2 != null)
+                            {
+                                objectRecord2.Display(hWindowControl.HalconWindow);
+                                objectRecord2.Dispose();
+                            }
                         }
                     }
                 }
@@ -410,11 +427,13 @@
 
                     if (dicContext.ContainsKey(IndexProcessName))
                     {
-                        ObjectRecord objectRecord3 = ((IProcess)dicContext[IndexProcessName]).Record;
-                        if (objectRecord3 != null)
+                        using (ObjectRecord objectRecord3 = ((IProcess)dicContext[IndexProcessName]).Record)
                         {
-                            objectRecord3.Display(hWindowControl.HalconWindow);
-                            objectRecord3.Dispose();
+                            if (objectRecord3 != null)
+                            {
+                                objectRecord3.Display(hWindowControl.HalconWindow);
+                                objectRecord3.Dispose();
+                            }
                         }
                     }
                 }
@@ -422,13 +441,13 @@
                 if (Result)
                 {
                     Msg = "杩愯鎴愬姛";
-                    HOperatorSet.SetColor(hWindowControl.HalconWindow, "green");
+                    hWindowControl.HalconWindow.SetColor("green");
                 }
                 else
-                    HOperatorSet.SetColor(hWindowControl.HalconWindow, "red");
+                    hWindowControl.HalconWindow.SetColor("red");
 
                 TAlgorithm.DispMsg(Msg, hWindowControl.HalconWindow, Result ? "green" : "red", 0, 0);
-                HOperatorSet.DumpWindowImage(out RecordImage, hWindowControl.HalconWindow);
+                RecordImage = hWindowControl.HalconWindow.DumpWindowImage();
                 #endregion
 
                 return true;
@@ -489,7 +508,38 @@
                 if (dicContext.TryGetValue(ProcessName, out IProcess obj)
                     && obj is IProcess process)
                 {
-                    process.InputImage = null;
+                    if (process.InputImage != null)
+                    {
+                        if (process.InputImage is HObject)
+                        {
+                            ((HObject)process.InputImage).Dispose();
+                        }
+                        else if (process.InputImage is Mat)
+                        {
+                            ((Mat)process.InputImage).Dispose();
+                        }
+                        else if (process.InputImage is Bitmap)
+                        {
+                            ((Bitmap)process.InputImage).Dispose();
+                        }
+                        process.InputImage = null;
+                    }
+                    if (process.OutputImage != null)
+                    {
+                        if (process.OutputImage is HObject)
+                        {
+                            ((HObject)process.OutputImage).Dispose();
+                        }
+                        else if (process.OutputImage is Mat)
+                        {
+                            ((Mat)process.OutputImage).Dispose();
+                        }
+                        else if (process.OutputImage is Bitmap)
+                        {
+                            ((Bitmap)process.OutputImage).Dispose();
+                        }
+                        process.OutputImage = null;
+                    }
                     UpdateInputs(process);
 
                     // 涓嶅悓鑺傜偣璺宠繃鐨勬柟寮忎笉鍚�
@@ -539,9 +589,8 @@
         [Node("鐩告満鍙栧浘", "鍙栧儚宸ュ叿", "Basic", "鐩告満鍙栧浘")]
         public void 鐩告満鍙栧浘(FlowNode node) { RunNodeAsync(node); }
 
-        [Node("Halcon2D鏂戠偣宸ュ叿", "Haclon2D宸ュ叿", "Basic", "Halcon2D鏂戠偣宸ュ叿")]
+        [Node("Halcon2D鏂戠偣宸ュ叿", "Halcon2D宸ュ叿", "Basic", "Halcon2D鏂戠偣宸ュ叿")]
         public void Halcon2D鏂戠偣宸ュ叿(FlowNode node) { RunNodeAsync(node); }
-
 
         #endregion
 

--
Gitblit v1.9.3