修改所有的保存图片

This commit is contained in:
TD 2025-04-02 20:14:06 +08:00
parent 67ba78f268
commit dbf412713b
3 changed files with 28 additions and 11 deletions

View File

@ -84,6 +84,8 @@ namespace DH.Devices.Vision
detectResult.SaveNGOriginal=detectConfig.SaveNGOriginal; detectResult.SaveNGOriginal=detectConfig.SaveNGOriginal;
detectResult.SaveOKDetect=detectConfig.SaveOKDetect; detectResult.SaveOKDetect=detectConfig.SaveOKDetect;
detectResult.SaveOKOriginal=detectConfig.SaveOKOriginal; detectResult.SaveOKOriginal=detectConfig.SaveOKOriginal;
Mat OriginImage = originImgSet.Clone();
detectResult.DetectionOriginImage = CopyBitmapWithLockBits(OriginImage.ToBitmap());
//detectResult.DetectionOriginImage = originImgSet.Clone().ToBitmap(); //detectResult.DetectionOriginImage = originImgSet.Clone().ToBitmap();
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
#region 1. #region 1.
@ -1003,7 +1005,7 @@ namespace DH.Devices.Vision
List<IShapeElement> detectionResultShapesClone = new List<IShapeElement>(detectionResultShapes); List<IShapeElement> detectionResultShapesClone = new List<IShapeElement>(detectionResultShapes);
DetectionDone(DetectionId, resultMask, detectionResultShapes); DetectionDone(DetectionId, resultMask, detectionResultShapes);
detectResult.DetectionOriginImage = CopyBitmapWithLockBits(resultMask); //detectResult.DetectionOriginImage = CopyBitmapWithLockBits(resultMask);
SaveDetectResultImageAsync(detectResult); SaveDetectResultImageAsync(detectResult);
// SaveDetectResultCSVAsync(detectResult); // SaveDetectResultCSVAsync(detectResult);
} }
@ -1108,15 +1110,15 @@ namespace DH.Devices.Vision
Bitmap preTreatedBitmap = detectResult.DetectionOriginImage.CopyBitmap(); Bitmap preTreatedBitmap = detectResult.DetectionOriginImage.CopyBitmap();
List<IShapeElement> detectionResultShapes = new List<IShapeElement>(detectResult.DetectionResultShapes); //List<IShapeElement> detectionResultShapes = new List<IShapeElement>(detectResult.DetectionResultShapes);
DetectResultDisplay resultDisplay = new DetectResultDisplay(detectResult, resultMask, displayTxt); //DetectResultDisplay resultDisplay = new DetectResultDisplay(detectResult, resultMask, displayTxt);
detectionResultShapes.Add(resultDisplay); //detectionResultShapes.Add(resultDisplay);
Bitmap resultMap = GetResultImage(resultMask, detectionResultShapes); Bitmap resultMap = GetResultImage(resultMask, detectResult.DetectionResultShapes);
resultDisplay.Dispose(); // resultDisplay.Dispose();
detectionResultShapes.Clear(); //detectionResultShapes.Clear();
Bitmap detectionFitImage = StaticHelper.HConnectBitmap(preTreatedBitmap, resultMap); Bitmap detectionFitImage = StaticHelper.HConnectBitmap(preTreatedBitmap, resultMap);
@ -1228,6 +1230,9 @@ namespace DH.Devices.Vision
{ {
g.DrawImage(baseImage, 0, 0); g.DrawImage(baseImage, 0, 0);
// 创建临时拷贝避免共享状态
// var tempElements = eleList.Select(e => e.Clone()).Cast<IShapeElement>().ToList();
eleList.ForEach(e => eleList.ForEach(e =>
{ {
e.State = ElementState.Normal; e.State = ElementState.Normal;
@ -1267,7 +1272,11 @@ namespace DH.Devices.Vision
} }
catch (Exception ex) catch (Exception ex)
{ {
// Logger.Error($"保存检测结果失败: {ex.Message}"); // Logger.Error($"保存检测结果失败: {ex.Message}");
}
finally {
GC.Collect();
GC.WaitForPendingFinalizers();
} }
} }
private void SaveNGImages(DetectStationResult result, string rootPath, string format) private void SaveNGImages(DetectStationResult result, string rootPath, string format)
@ -1322,7 +1331,7 @@ namespace DH.Devices.Vision
using (Bitmap resultMask = result.DetectionOriginImage.CopyBitmap()) using (Bitmap resultMask = result.DetectionOriginImage.CopyBitmap())
using (Bitmap preTreatedBitmap = result.DetectionOriginImage.CopyBitmap()) using (Bitmap preTreatedBitmap = result.DetectionOriginImage.CopyBitmap())
{ {
var detectionResultShapes = new List<IShapeElement>(result.DetectionResultShapes) var detectionResultShapes = new List<IShapeElement>()
{ {
new DetectResultDisplay(result, resultMask, displayTxt) new DetectResultDisplay(result, resultMask, displayTxt)
}; };
@ -1333,7 +1342,7 @@ namespace DH.Devices.Vision
string prefix = Path.Combine(rootPath, "OKFitImages", result.ResultLabel); string prefix = Path.Combine(rootPath, "OKFitImages", result.ResultLabel);
Directory.CreateDirectory(prefix); Directory.CreateDirectory(prefix);
string fullname = Path.Combine(prefix, $"{result.Pid}_OKFitImage_{result.DetectName}_{result.Id}.{format}"); string fullname = Path.Combine(prefix, $"{result.Pid}_OKFitImage_{result.DetectName}_{result.Id}.{format}");
SaveImageAsync(fullname, result.DetectionOriginImage, result.ImageFormat); SaveImageAsync(fullname, detectionFitImage, result.ImageFormat);
} }
} }

View File

@ -10,6 +10,14 @@
<Platforms>AnyCPU;x64</Platforms> <Platforms>AnyCPU;x64</Platforms>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<Optimize>False</Optimize>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Optimize>False</Optimize>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\DH.Commons\DH.Commons.csproj" /> <ProjectReference Include="..\DH.Commons\DH.Commons.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -580,7 +580,7 @@ namespace DH.UI.Model.Winform
/// </summary> /// </summary>
[Browsable(false)] [Browsable(false)]
public Rectangle BaseRectangle { get; set; } public Rectangle BaseRectangle { get; set; }
EnumHelper.ElementState IShapeElement.State { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public abstract bool IsMouseHover(PointF p); public abstract bool IsMouseHover(PointF p);