public override bool SaveResult(IMeasurementResult result, string inputFileName, string ext) { try { Type t = result.GetType(); CultureInfo cultInfo = CultureInfo.InvariantCulture; PropertyInfo[] props = t.GetProperties(); foreach (PropertyInfo prop in props) { object obj = prop.GetValue(result); if (!(obj is Image <Gray, double>)) { continue; } string fileNameBase = Path.GetFileNameWithoutExtension(inputFileName); string finalOutputName = Path.Combine(OutputFolder, "LineScans", $"{fileNameBase}_{_prefix}_{ext}_{prop.Name}.csv"); if (!Directory.Exists(Path.GetDirectoryName(finalOutputName))) { Directory.CreateDirectory(Path.GetDirectoryName(finalOutputName)); } using (StreamWriter sw = new StreamWriter(finalOutputName)) { double[,,] data = (obj as Image <Gray, double>).Data; for (int i = 0; i < data?.Length; i++) { sw.WriteLine(data[0, i, 0].ToString(cultInfo)); } } _logger?.Trace("Image " + finalOutputName + " saved.", ClassName); } } catch (Exception ex) { _logger?.ErrorLog($"Exception in CsvResultSaver-SaveResult: {ex}", ClassName); return(false); } return(true); }
public override bool SaveResult(IMeasurementResult result, string inputFileName, string ext) { try { Type t = result.GetType(); PropertyInfo[] props = t.GetProperties(); foreach (PropertyInfo prop in props) { object obj = prop.GetValue(result); if (!(obj is Image <Gray, float>)) { continue; } string fileNameBase = Path.GetFileNameWithoutExtension(inputFileName); string finalOutputName = Path.Combine(OutputFolder, $"{fileNameBase}_{_prefix}_{prop.Name}.png"); _tempImage1 = (obj as Image <Gray, float>).Convert <Gray, byte>(); _tempImage1?.Save(finalOutputName); _logger?.TraceLog("Image " + prop.Name + " " + " is saved", ClassName); } } catch (Exception ex) { _logger?.ErrorLog($"Exception occured: {ex}", ClassName); return(false); } finally { _tempImage1?.Dispose(); } return(true); }
public override bool SaveResult(IMeasurementResult result, string inputFileName, string ext) { try { string fileNameBase = Path.GetFileNameWithoutExtension(inputFileName); string finalStatOutputName = Path.Combine(OutputFolder, $"{_prefix}_{ext}.csv"); Type t = result.GetType(); List <PropertyInfo> propLis = t.GetProperties().ToList(); List <PropertyInfo> propList = new List <PropertyInfo>(); foreach (var prop in propLis) { object obj = prop.GetValue(result); if (!(obj is Image <Gray, double>)) { propList.Add(prop); } } List <string> propString = propList.Select(a => a.Name).ToList(); if (!File.Exists(finalStatOutputName)) { using (FileStream fs = new FileStream(finalStatOutputName, FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { foreach (PropertyInfo prop in propList) { sw.Write(prop.Name); if (propList.IndexOf(prop) != propList.Count - 1) { sw.Write(","); } } } } } else { List <string> existingProps; using (FileStream fs = new FileStream(finalStatOutputName, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { string text = sr.ReadLine(); existingProps = text?.Split(',').ToList() ?? new List <string>(); } } if (!propString.SequenceEqual(existingProps)) { using (FileStream fs = new FileStream(finalStatOutputName, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(Environment.NewLine); foreach (PropertyInfo prop in propList) { sw.Write(prop.Name); if (propList.IndexOf(prop) != propList.Count - 1) { sw.Write(","); } } } } } } using (FileStream fs = new FileStream(finalStatOutputName, FileMode.Append)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write(Environment.NewLine); foreach (PropertyInfo prop in propList) { object obj = prop.GetValue(result); if (!(obj is Image <Gray, double>)) { sw.Write((obj ?? string.Empty)); if (propList.IndexOf(prop) != propList.Count - 1) { sw.Write(","); } } } } } } catch (Exception ex) { _logger?.ErrorLog($"Exception occured: {ex}", ClassName); return(false); } return(true); }