MainForm.cs 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  1. namespace OINA.Extender.Testharness
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.ComponentModel;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.Serialization;
  13. using System.Text;
  14. using System.Threading;
  15. using System.Windows.Forms;
  16. using OINA.Extender.Acquisition;
  17. using OINA.Extender.Acquisition.Ed;
  18. using OINA.Extender.Controls;
  19. using OINA.Extender.Controls.Common;
  20. using OINA.Extender.Controls.Image;
  21. using OINA.Extender.Controls.Spectrum;
  22. using OINA.Extender.Data;
  23. using OINA.Extender.Data.Ed;
  24. using OINA.Extender.Data.Image;
  25. using OINA.Extender.Processing;
  26. using OINA.Extender.Processing.Quant;
  27. using OINA.Extender.Testharness.Properties;
  28. using WindowsMedia = System.Windows.Media;
  29. /// <summary>
  30. /// MainForm Class
  31. /// </summary>
  32. public partial class MainForm : Form
  33. {
  34. /// <summary>
  35. /// List of EdSpectrum objects
  36. /// </summary>
  37. private readonly Dictionary<IEdSpectrum, AcquisitionItem> edSpectrumList = new Dictionary<IEdSpectrum, AcquisitionItem>();
  38. /// <summary>
  39. /// List of ElectronImage objects
  40. /// </summary>
  41. private readonly List<IElectronImage> electronImageList = new List<IElectronImage>();
  42. /// <summary>
  43. /// SpectrumViewer object
  44. /// </summary>
  45. private SpectrumViewer spectrumViewer;
  46. /// <summary>
  47. /// ElementCombo object for exclude element
  48. /// </summary>
  49. private ElementCombo excludeElementCombo;
  50. /// <summary>
  51. /// ElementCombo object for deconvolution element
  52. /// </summary>
  53. private ElementCombo deconvolutionElementCombo;
  54. /// <summary>
  55. /// The periodic table control
  56. /// </summary>
  57. private PeriodicTableControl periodicTableControl;
  58. /// <summary>
  59. /// imageViewer object
  60. /// </summary>
  61. private ImageViewer imageViewer;
  62. /// <summary>
  63. /// The comparison spectra
  64. /// </summary>
  65. private List<IEdSpectrum> comparisonSpectra;
  66. /// <summary>
  67. /// The periodic table control visual settings window
  68. /// </summary>
  69. private Form periodicTableVisualSettings;
  70. /// <summary>
  71. /// The element combo control visual settings window
  72. /// </summary>
  73. private Form elementComboVisualSettings;
  74. /// <summary>
  75. /// The spectrum viewer control visual settings window
  76. /// </summary>
  77. private Form spectrumViewerVisualSettings;
  78. /// <summary>
  79. /// The image viewer control visual settings window
  80. /// </summary>
  81. private Form imageViewVisualSettingsForm;
  82. /// <summary>
  83. /// The brightness/contrast dialog visual settings window
  84. /// </summary>
  85. private Form brightnessContrastDialogVisualSettings;
  86. /// <summary>
  87. /// The details dialog visual settings window
  88. /// </summary>
  89. private Form detailsDialogVisualSettingsForm;
  90. /// <summary>
  91. /// MainForm Constructor
  92. /// </summary>
  93. public MainForm()
  94. {
  95. this.InitializeComponent();
  96. this.LoadComparisonSpectra();
  97. this.cbCompareColor.DataSource = Enum.GetValues(typeof(KnownColor));
  98. this.imageToolComboBox.DataSource = Enum.GetValues(typeof(ImageInteractionTool));
  99. this.spectrumInteractionToolComboBox.DataSource = Enum.GetValues(typeof(InteractionTool));
  100. // Binding spectrum acquisition controller events
  101. OIHelper.EdSpectrumAcquisitionController.ExperimentStarted += this.OnEdSpectrumExperimentStarted;
  102. OIHelper.EdSpectrumAcquisitionController.ExperimentFinished += this.OnEdSpectrumExperimentFinished;
  103. // Binding image acquisition controller events
  104. OIHelper.ImageAcquisitionController.ExperimentStarted += this.OnImageExperimentStarted;
  105. OIHelper.ImageAcquisitionController.ExperimentFinished += this.OnImageExperimentFinished;
  106. // Initial UI contents
  107. this.InitialSpectrumViewerElementHost();
  108. this.InitialImageViewerElementHost();
  109. this.InitialPeriodicTableElementHost();
  110. this.InitialSpectrumSettingsContent();
  111. this.InitialImageSettingsContent();
  112. this.InitialTimers();
  113. this.InitialProcessingSettingsContent();
  114. this.InitializeEnabledElementsMenuItems();
  115. this.cbCompareColor.SelectedItem = GetRandomColor();
  116. this.spectrumInteractionToolComboBox.SelectedItem = this.spectrumViewer.InteractionTool;
  117. this.UpdateHighlightedElementInTable();
  118. }
  119. /// <summary>
  120. /// Gets the random color.
  121. /// </summary>
  122. /// <returns>A random color</returns>
  123. private static KnownColor GetRandomColor()
  124. {
  125. Random randomGen = new Random();
  126. KnownColor[] colors = Enum.GetValues(typeof(KnownColor)) as KnownColor[];
  127. int index = randomGen.Next(colors.Length);
  128. return colors[index];
  129. }
  130. /// <summary>
  131. /// Gets a value indicating whether this instance is acquiring.
  132. /// </summary>
  133. /// <value>
  134. /// <c>true</c> if this instance is acquiring; otherwise, <c>false</c>.
  135. /// </value>
  136. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "n/a")]
  137. private bool IsAcquiring
  138. {
  139. get { return OIHelper.EdSpectrumAcquisitionController.IsAcquiring; }
  140. }
  141. #region Status Event Handlers
  142. /// <summary>
  143. /// OnAcquisitionStatusTimerTicked
  144. /// </summary>
  145. /// <param name="sender">sender object</param>
  146. /// <param name="e">EvenArgs</param>
  147. private void OnAcquisitionStatusTimerTicked(object sender, EventArgs e)
  148. {
  149. if (OIHelper.EdSpectrumAcquisitionController.IsAcquiring || OIHelper.ImageAcquisitionController.IsAcquiring)
  150. {
  151. this.lblAcquisitionStatus.Visible = !this.lblAcquisitionStatus.Visible;
  152. }
  153. else
  154. {
  155. this.lblAcquisitionStatus.Visible = false;
  156. }
  157. }
  158. /// <summary>
  159. /// OnIdentifyTimerTicked
  160. /// </summary>
  161. /// <param name="sender">sender object</param>
  162. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  163. private void OnIdentifyTimerTicked(object sender, EventArgs e)
  164. {
  165. this.DoAutoID();
  166. }
  167. /// <summary>
  168. /// Called when [ed spectrum experiment started].
  169. /// </summary>
  170. /// <param name="sender">The sender.</param>
  171. /// <param name="e">The <see cref="AcquisitionFinishedEventArgs{IEdSpectrum}"/> instance containing the event data.</param>
  172. private void OnEdSpectrumExperimentStarted(object sender, AcquisitionStartedEventArgs<IEdSpectrum> e)
  173. {
  174. var edSpectrum = e.Value;
  175. // execute on UI thread
  176. this.BeginInvoke((Action)delegate
  177. {
  178. this.spectrumViewer.Spectrum = edSpectrum;
  179. // Clear periodic table included and excluded elements
  180. this.periodicTableControl.ClearAll();
  181. // Update Known Elements
  182. this.periodicTableControl.IncludeElements(OIHelper.AutoIdSettings.KnownElements.ToArray());
  183. this.tmrIdentifyElements.Tick += this.OnIdentifyTimerTicked;
  184. this.tmrIdentifyElements.Enabled = true;
  185. });
  186. edSpectrum.PropertyChanged += this.OnPropertyChanged;
  187. }
  188. /// <summary>
  189. /// Called when [image experiment started].
  190. /// </summary>
  191. /// <param name="sender">The sender.</param>
  192. /// <param name="e">The instance containing the event data.</param>
  193. private void OnImageExperimentStarted(object sender, AcquisitionStartedEventArgs<IElectronImage[]> e)
  194. {
  195. // execute on UI thread
  196. this.BeginInvoke((Action)delegate
  197. {
  198. this.imageViewer.DisplayImage = e.Value[0];
  199. });
  200. }
  201. /// <summary>
  202. /// OnExperimentFinished
  203. /// </summary>
  204. /// <param name="sender">sender object</param>
  205. /// <param name="e">The instance containing the event data.</param>
  206. private void OnEdSpectrumExperimentFinished(object sender, AcquisitionFinishedEventArgs<IEdSpectrum> e)
  207. {
  208. e.Value.PropertyChanged -= this.OnPropertyChanged;
  209. this.tmrIdentifyElements.Tick -= this.OnIdentifyTimerTicked;
  210. this.tmrIdentifyElements.Enabled = false;
  211. // execute on UI thread
  212. this.BeginInvoke((Action)delegate
  213. {
  214. this.DoAutoID();
  215. this.UpdatePeriodicTable();
  216. this.DoQuant();
  217. if (this.edSpectrumList.ContainsKey(e.Value))
  218. {
  219. this.AcquisitionQueueListBox.Items.Remove(this.edSpectrumList[e.Value]);
  220. this.edSpectrumList.Remove(e.Value);
  221. }
  222. // *** If this is the last experiment, call EndMultipleAcquisition on the Acquisition
  223. // *** controller, to re-enable external scan switching
  224. if (!this.edSpectrumList.Any())
  225. {
  226. OIHelper.EdSpectrumAcquisitionController.EndMultipleAcquisition();
  227. }
  228. });
  229. }
  230. /// <summary>
  231. /// Called when [image experiment finished].
  232. /// </summary>
  233. /// <param name="sender">The sender.</param>
  234. /// <param name="e">The instance containing the event data.</param>
  235. private void OnImageExperimentFinished(object sender, AcquisitionFinishedEventArgs<IElectronImage[]> e)
  236. {
  237. // execute on UI thread
  238. this.BeginInvoke((Action)delegate
  239. {
  240. e.Value.ToList().ForEach(i =>
  241. {
  242. this.electronImageList.Remove(i);
  243. this.ImageAcquisitionQueueListBox.Items.Remove(i);
  244. });
  245. });
  246. }
  247. /// <summary>
  248. /// OnPropertyChanged
  249. /// </summary>
  250. /// <param name="sender">Event source</param>
  251. /// <param name="e">Event arguments</param>
  252. private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
  253. {
  254. var edSpectrum = sender as IEdSpectrum;
  255. switch (e.PropertyName)
  256. {
  257. case "RealTimeSeconds":
  258. this.Invoke((MethodInvoker)delegate
  259. {
  260. this.textRealTimeSeconds.Text = edSpectrum.RealTimeSeconds.ToString(@"0.000", CultureInfo.InvariantCulture);
  261. });
  262. break;
  263. case "LiveTimeSeconds":
  264. this.Invoke((MethodInvoker)delegate
  265. {
  266. this.textLiveTimeSeconds.Text = edSpectrum.LiveTimeSeconds.ToString(@"0.000", CultureInfo.InvariantCulture);
  267. });
  268. break;
  269. case "OutputRate":
  270. this.Invoke((MethodInvoker)delegate
  271. {
  272. this.outputStatus.Text = OIHelper.MonitoringController.EdStatus.Values.Sum(status => status.OutputRate).ToString(CultureInfo.InvariantCulture) + @"cps";
  273. });
  274. break;
  275. case "DeadTime":
  276. this.Invoke((MethodInvoker)delegate
  277. {
  278. this.deadtimeStatus.Text = OIHelper.MonitoringController.EdStatus.Values.Max(status => status.DeadTime).ToString(CultureInfo.InvariantCulture) + @"%";
  279. });
  280. break;
  281. }
  282. }
  283. #endregion
  284. #region Update UI status
  285. /// <summary>
  286. /// InitialSpectrumSettingsContent
  287. /// </summary>
  288. private void InitialSpectrumSettingsContent()
  289. {
  290. if (OIHelper.EdSpectrumSettings.EdCapabilities.HasHardwareConnection)
  291. {
  292. this.textSpectrumLabel.Text = @"Spectrum 1";
  293. this.cbAcquisitionMode.Items.Add(EdAcquireMode.LiveTime);
  294. this.cbAcquisitionMode.Items.Add(EdAcquireMode.RealTime);
  295. this.cbAcquisitionMode.SelectedIndex = 0;
  296. this.AcquisitionTimeNumericUpDown.Value = 10;
  297. this.cbProcessTime.DataSource = OIHelper.EdSpectrumSettings.EdCapabilities.AllowedProcessTimes;
  298. this.cbProcessTime.SelectedIndex = 3;
  299. this.cbEnergyRange.DataSource = OIHelper.EdSpectrumSettings.EdCapabilities.AllowedEnergyRanges;
  300. this.cbEnergyRange.SelectedIndex = 1;
  301. this.cbChannelNumber.DataSource = OIHelper.EdSpectrumSettings.EdCapabilities.AllowedNumberOfChannels;
  302. this.cbChannelNumber.SelectedIndex = 1;
  303. }
  304. }
  305. /// <summary>
  306. /// InitialImageSettingsContent
  307. /// </summary>
  308. private void InitialImageSettingsContent()
  309. {
  310. var settings = OIHelper.ImageSettings;
  311. if (settings.ImageCapabilities.HasHardwareConnection)
  312. {
  313. this.cbImageScanSize.DataSource = OIHelper.GetScanSizes(
  314. settings.ScanCapabilities.MinimumPixelSize,
  315. settings.ScanCapabilities.MaximumPixelSize);
  316. this.cbImageScanSize.SelectedIndex = this.cbImageScanSize.Items.Count > 4 ? 4 : this.cbImageScanSize.Items.Count - 1;
  317. this.cbInputSignal.DataSource = settings.ImageCapabilities.AllowedInputSources;
  318. this.cbInputSignal.SelectedIndex = 0;
  319. this.tbNumberOfFrames.Text = @"1";
  320. this.tbDwellTime.Text = @"40";
  321. }
  322. }
  323. /// <summary>
  324. /// Initial Timers
  325. /// </summary>
  326. private void InitialTimers()
  327. {
  328. this.tmrAcquisitionStatus.Enabled = true;
  329. this.tmrAcquisitionStatus.Interval = 1000;
  330. this.tmrAcquisitionStatus.Tick += this.OnAcquisitionStatusTimerTicked;
  331. this.tmrIdentifyElements.Enabled = false;
  332. this.tmrIdentifyElements.Interval = 1000;
  333. }
  334. /// <summary>
  335. /// InitialProcessingSettingsContent
  336. /// </summary>
  337. private void InitialProcessingSettingsContent()
  338. {
  339. this.rbAllElements.Checked = true;
  340. this.tbCombinedElement.Enabled = false;
  341. this.tbCombinedElement.Text = @"8";
  342. this.tbNumberOfIons.Enabled = false;
  343. this.tbNumberOfIons.Text = @"3.0";
  344. this.deconvolutionElementCombo = new ElementCombo();
  345. this.ehDeconvolutionElement.Child = this.deconvolutionElementCombo;
  346. this.deconvolutionElementCombo.DisplayFormat = ElementComboDisplayFormat.ShowSymbol;
  347. foreach (int i in OIHelper.SEMQuantSettings.DeconvolutionElements)
  348. {
  349. this.tbDeconvolutionElementList.Text += i.ToString(CultureInfo.InvariantCulture) + System.Environment.NewLine;
  350. }
  351. this.cbThresholding.Checked = false;
  352. this.tbSigmaLevel.Enabled = false;
  353. this.tbSigmaLevel.Text = @"3.0";
  354. this.cbIsCoated.Checked = OIHelper.SEMQuantSettings.SampleCoating.IsCoated;
  355. this.tbCoatingElement.Enabled = OIHelper.SEMQuantSettings.SampleCoating.IsCoated;
  356. this.tbCoatingElement.Text = OIHelper.SEMQuantSettings.SampleCoating.CoatingElement.ToString(CultureInfo.InvariantCulture);
  357. this.tbDensity.Enabled = OIHelper.SEMQuantSettings.SampleCoating.IsCoated;
  358. this.tbDensity.Text = OIHelper.SEMQuantSettings.SampleCoating.Density.ToString(CultureInfo.InvariantCulture);
  359. this.tbThickness.Enabled = OIHelper.SEMQuantSettings.SampleCoating.IsCoated;
  360. this.tbThickness.Text = OIHelper.SEMQuantSettings.SampleCoating.Thickness.ToString(CultureInfo.InvariantCulture);
  361. this.excludeElementCombo = new ElementCombo();
  362. this.ehExcludeElement.Child = this.excludeElementCombo;
  363. this.excludeElementCombo.DisplayFormat = ElementComboDisplayFormat.ShowName;
  364. this.excludeElementCombo.PropertyChanged += this.ExcludeElementCombo_PropertyChanged;
  365. this.UpdateElementsList(this.periodicTableControl.ExcludedElements, this.tbExcludedElementList);
  366. this.UpdateElementsList(this.periodicTableControl.IncludedElements, this.tbIncludedElementList);
  367. this.UpdateElementsList(this.periodicTableControl.UnsetElements, this.tbUnsetElementList);
  368. }
  369. /// <summary>
  370. /// InitialSpectrumViewerElementHost
  371. /// </summary>
  372. private void InitialSpectrumViewerElementHost()
  373. {
  374. this.spectrumViewer = new SpectrumViewer();
  375. this.ehSpectrumViewer.Child = this.spectrumViewer;
  376. this.spectrumViewer.IsXAxisAutoScaleEnabled = false;
  377. this.spectrumViewer.IsYAxisAutoScaleEnabled = true;
  378. }
  379. /// <summary>
  380. /// InitialImageViewerElementHost
  381. /// </summary>
  382. private void InitialImageViewerElementHost()
  383. {
  384. this.imageViewer = new ImageViewer();
  385. this.ehImageViewer.Child = this.imageViewer;
  386. }
  387. /// <summary>
  388. /// Initials the periodic table element host.
  389. /// </summary>
  390. private void InitialPeriodicTableElementHost()
  391. {
  392. this.periodicTableControl = new PeriodicTableControl();
  393. this.periodicTableElementHost.Child = this.periodicTableControl;
  394. // Set the Periodic Table BackColor (0xFF07366B) same as Spectrum Viewer
  395. this.periodicTableElementHost.BackColor = Color.FromArgb(255, 7, 54, 107);
  396. foreach (object knownColor in Enum.GetValues(typeof(System.Drawing.KnownColor)))
  397. {
  398. this.periodicTableBackgroundToolStripComboBox.Items.Add(knownColor);
  399. }
  400. ((INotifyCollectionChanged)this.periodicTableControl.ExcludedElements).CollectionChanged += this.PeriodicTableExcludedElements_CollectionChanged;
  401. ((INotifyCollectionChanged)this.periodicTableControl.IncludedElements).CollectionChanged += this.PeriodicTableIncludedElements_CollectionChanged;
  402. ((INotifyCollectionChanged)this.periodicTableControl.UnsetElements).CollectionChanged += this.PeriodicTableUnsetElements_CollectionChanged;
  403. }
  404. /// <summary>
  405. /// Update the Periodic Table content after acquisition finished
  406. /// </summary>
  407. private void UpdatePeriodicTable()
  408. {
  409. this.periodicTableControl.IncludeElements(this.spectrumViewer.Spectrum.IdentifiedElements.ToArray());
  410. }
  411. /// <summary>
  412. /// Updates the elements list.
  413. /// </summary>
  414. /// <param name="data">The data.</param>
  415. /// <param name="list">The list.</param>
  416. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Ignore")]
  417. private void UpdateElementsList(IEnumerable<int> data, TextBox list)
  418. {
  419. list.Clear();
  420. foreach (int i in data.OrderBy(x => x).ToArray<int>())
  421. {
  422. list.Text += ElementProperties.GetElementName(i) + System.Environment.NewLine;
  423. }
  424. }
  425. #endregion
  426. #region Control acquisition from UI
  427. /// <summary>
  428. /// StartAcquisition button click handler
  429. /// </summary>
  430. /// <param name="sender">sender object</param>
  431. /// <param name="e">EventArgs</param>
  432. private void StartAcquisitionButton_Click(object sender, EventArgs e)
  433. {
  434. this.spectrumViewer.ClearComparisonSpectra();
  435. this.AcquireSpectrum(false);
  436. }
  437. /// <summary>
  438. /// ResumeAcquisition button click handler
  439. /// </summary>
  440. /// <param name="sender">sender object</param>
  441. /// <param name="e">EventArgs</param>
  442. private void ResumeAcquisitionButton_Click(object sender, EventArgs e)
  443. {
  444. if (this.spectrumViewer.Spectrum != null && !this.IsAcquiring)
  445. {
  446. this.AcquireSpectrum(true);
  447. }
  448. }
  449. /// <summary>
  450. /// Start Acquire Spectrum Process
  451. /// </summary>
  452. /// <param name="isResume">true if is resume</param>
  453. private void AcquireSpectrum(bool isResume)
  454. {
  455. IEdSpectrum edSpectrum = null;
  456. // Spectrum acquisition settings confirm
  457. var edSpectrumSettings = OIHelper.EdSpectrumSettings;
  458. var edSpectrumAcquisitionController = OIHelper.EdSpectrumAcquisitionController;
  459. edSpectrumSettings.EdSettings.AcquisitionMode = (EdAcquireMode)this.cbAcquisitionMode.SelectedItem;
  460. edSpectrumSettings.EdSettings.AcquisitionTime = TimeSpan.FromSeconds((int)this.AcquisitionTimeNumericUpDown.Value);
  461. edSpectrumSettings.EdSettings.ProcessTime = (int)this.cbProcessTime.SelectedValue;
  462. edSpectrumSettings.EdSettings.EnergyRange = (int)this.cbEnergyRange.SelectedValue;
  463. edSpectrumSettings.EdSettings.NumberOfChannels = (int)this.cbChannelNumber.SelectedValue;
  464. edSpectrumSettings.ScanSettings.AcquisitionRegion.CreateMicroscopeRegion();
  465. // SpectrumViewer settings confirm
  466. this.spectrumViewer.MaximumEnergy = edSpectrumSettings.EdSettings.EnergyRange;
  467. // If Ed hardware is not ready, the software will crash.
  468. if (edSpectrumAcquisitionController.IsEdHardwareReady(edSpectrumSettings))
  469. {
  470. // *** If this is the first acquisition, call BeginMultipleAcquisition
  471. // *** on the AcquisitionController to suppress external scan switching
  472. if (!this.edSpectrumList.Any())
  473. {
  474. edSpectrumAcquisitionController.BeginMultipleAcquisition();
  475. }
  476. if (!isResume)
  477. {
  478. // Start spectrum acquisition
  479. try
  480. {
  481. edSpectrum = edSpectrumAcquisitionController.StartAcquisition(edSpectrumSettings);
  482. edSpectrum.Label = string.Format(CultureInfo.CurrentCulture, @"{0} ({1:HH:mm:ss})", this.textSpectrumLabel.Text, DateTime.Now);
  483. this.edSpectrumList.Add(edSpectrum, new AcquisitionItem(edSpectrum, OIHelper.EdSpectrumSettings.EdSettings.AcquisitionMode, OIHelper.EdSpectrumSettings.EdSettings.AcquisitionTime.TotalMilliseconds, this.AcquisitionQueueListBox));
  484. this.AcquisitionQueueListBox.Items.Add(this.edSpectrumList[edSpectrum]);
  485. }
  486. catch (InvalidSettingsException invalidSettingsException)
  487. {
  488. MessageBox.Show(invalidSettingsException.Message, Resources.CannotStartAcquisitionTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
  489. }
  490. catch (AcquisitionStartException acquisitionStartException)
  491. {
  492. MessageBox.Show(acquisitionStartException.Message, Resources.CannotStartAcquisitionTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
  493. }
  494. }
  495. else
  496. {
  497. edSpectrum = this.spectrumViewer.Spectrum;
  498. edSpectrumAcquisitionController.ResumeAcquisition(edSpectrumSettings, edSpectrum);
  499. this.edSpectrumList.Add(edSpectrum, new AcquisitionItem(edSpectrum, OIHelper.EdSpectrumSettings.EdSettings.AcquisitionMode, OIHelper.EdSpectrumSettings.EdSettings.AcquisitionTime.TotalMilliseconds, this.AcquisitionQueueListBox));
  500. this.AcquisitionQueueListBox.Items.Add(this.edSpectrumList[edSpectrum]);
  501. }
  502. }
  503. }
  504. /// <summary>
  505. /// StopAcquisition button click handler
  506. /// </summary>
  507. /// <param name="sender">sender object</param>
  508. /// <param name="e">EventArgs</param>
  509. private void StopAcquisitionButton_Click(object sender, EventArgs e)
  510. {
  511. if (OIHelper.EdSpectrumAcquisitionController.IsAcquiring == true)
  512. {
  513. OIHelper.EdSpectrumAcquisitionController.StopAcquisition();
  514. }
  515. }
  516. /// <summary>
  517. /// Handles the Click event of Calculate button.
  518. /// </summary>
  519. /// <param name="sender">The source of the event.</param>
  520. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  521. private void CalculateButton_Click(object sender, EventArgs e)
  522. {
  523. try
  524. {
  525. int atomicNum = ElementProperties.GetAtomicNumberByElementSymbol(this.tbIdentifiedElement.SelectedText);
  526. var peakParameters = OIHelper.EdSpectrumProcessing.CalculatePeakParameters(this.spectrumViewer.Spectrum, atomicNum);
  527. this.tbEnergy.Text = peakParameters.Centre.ToString(CultureInfo.InvariantCulture);
  528. this.tbFWHM.Text = peakParameters.FWHM.ToString(CultureInfo.InvariantCulture);
  529. }
  530. catch (NullReferenceException)
  531. {
  532. }
  533. catch (FormatException)
  534. {
  535. }
  536. catch (InvalidOperationException)
  537. {
  538. }
  539. }
  540. /// <summary>
  541. /// Handles the Click event of StartImageScan button.
  542. /// </summary>
  543. /// <param name="sender">The source of the event.</param>
  544. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  545. private void StartImageScanButton_Click(object sender, EventArgs e)
  546. {
  547. var imageSettings = OIHelper.ImageSettings.ImageSettings;
  548. var imageCapabilities = OIHelper.ImageSettings.ImageCapabilities;
  549. var scanSettings = OIHelper.ImageSettings.ScanSettings;
  550. var imageAcquisitionController = OIHelper.ImageAcquisitionController;
  551. this.tbImageData.Text = string.Empty;
  552. this.tbPointX.Text = string.Empty;
  553. this.tbPointY.Text = string.Empty;
  554. var value = int.Parse(this.tbDwellTime.Text, CultureInfo.InvariantCulture);
  555. if (value > imageCapabilities.MaximumImageDwellMicroseconds)
  556. {
  557. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MaximumImageDwellMicroseconds;
  558. }
  559. if (value < imageCapabilities.MinimumImageDwellMicroseconds)
  560. {
  561. imageSettings.DwellTimeMicroSeconds = imageCapabilities.MinimumImageDwellMicroseconds;
  562. }
  563. else
  564. {
  565. imageSettings.DwellTimeMicroSeconds = value;
  566. }
  567. // *** Disable all input sources before enabling the one that the user has chosen
  568. imageSettings.InputSources.ToList().ForEach(i => imageSettings.EnableInputSource(i.Key, false));
  569. imageSettings.EnableInputSource((ImageInputSources)this.cbInputSignal.SelectedItem, true);
  570. scanSettings.FrameCount = int.Parse(this.tbNumberOfFrames.Text, CultureInfo.InvariantCulture);
  571. var pixelSize = 1d / double.Parse(this.cbImageScanSize.Text, CultureInfo.InvariantCulture);
  572. scanSettings.AcquisitionRegion.CreateFullFieldRegion(pixelSize);
  573. try
  574. {
  575. var images = imageAcquisitionController.StartAcquisition(OIHelper.ImageSettings).ToList();
  576. images.ForEach(i =>
  577. {
  578. i.Label = string.Format(CultureInfo.CurrentCulture, @"{0} ({1:HH:mm:ss})", @"ElectronImage", DateTime.Now);
  579. this.electronImageList.Add(i);
  580. this.ImageAcquisitionQueueListBox.Items.Add(i);
  581. });
  582. }
  583. catch (InvalidSettingsException exception)
  584. {
  585. var sb = new StringBuilder(@"Invalid settings have been supplied:");
  586. sb.AppendLine();
  587. exception.ValidationResults.ValidationErrors
  588. .ToList()
  589. .ForEach(ve => sb.AppendFormat(CultureInfo.CurrentCulture, @"{0} {1}", Environment.NewLine, ve));
  590. MessageBox.Show(sb.ToString(), @"Invalid settings", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  591. }
  592. }
  593. /// <summary>
  594. /// Handles the Click event of StopImageScan button.
  595. /// </summary>
  596. /// <param name="sender">The source of the event.</param>
  597. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  598. private void StopImageScanButton_Click(object sender, EventArgs e)
  599. {
  600. if (OIHelper.ImageAcquisitionController.IsAcquiring)
  601. {
  602. OIHelper.ImageAcquisitionController.StopAcquisition();
  603. }
  604. }
  605. /// <summary>
  606. /// Handles the Click event of GetPixelData button.
  607. /// </summary>
  608. /// <param name="sender">The source of the event.</param>
  609. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  610. private void GetPixelDataButton_Click(object sender, EventArgs e)
  611. {
  612. if ((!OIHelper.ImageAcquisitionController.IsAcquiring) & (this.imageViewer.DisplayImage != null))
  613. {
  614. var electronImage = this.imageViewer.DisplayImage as IElectronImage;
  615. if (electronImage != null)
  616. {
  617. try
  618. {
  619. int x = int.Parse(this.tbPointX.Text, CultureInfo.InvariantCulture);
  620. int y = int.Parse(this.tbPointY.Text, CultureInfo.InvariantCulture);
  621. var stringBuilder = new StringBuilder();
  622. stringBuilder.AppendFormat(CultureInfo.CurrentCulture, @" Point({0},{1}), Value: {2}", x, y, electronImage.GetData(new Point(x, y)));
  623. stringBuilder.Append(Environment.NewLine);
  624. this.tbImageData.Text = stringBuilder.ToString();
  625. }
  626. catch (ArgumentOutOfRangeException)
  627. {
  628. //// stringBuilder - The length of the expanded string would exceed MaxCapacity.
  629. }
  630. catch (FormatException)
  631. {
  632. //// int.Parse - The x or y coordinates are not in the correct format.
  633. }
  634. }
  635. }
  636. }
  637. #endregion
  638. #region ToolStripMenuItem handlers
  639. /// <summary>
  640. /// Handles the Click event of the elementComboVisualSettingsToolStripMenuItem control.
  641. /// </summary>
  642. /// <param name="sender">The source of the event.</param>
  643. /// <param name="e">instance containing the event data.</param>
  644. private void ExportSpectrumAsToolStripMenuItem_Click(object sender, EventArgs e)
  645. {
  646. string filter = @"Bitmap (*.bmp)|*.bmp|PNG (*.png)|*.png|TIFF (*.tif)|*.tif|JPEG (*.jpg)|*.jpg";
  647. string title = @"Export Spectrum As...";
  648. try
  649. {
  650. short width = (short)this.spectrumViewer.ActualWidth;
  651. short height = (short)this.spectrumViewer.ActualHeight;
  652. Bitmap bitmap = OIHelper.EdSpectrumProcessing.CreateBitmap(this.spectrumViewer.Spectrum, width, height);
  653. using (SaveFileDialog exportSpectrumDialog = new SaveFileDialog())
  654. {
  655. exportSpectrumDialog.Filter = filter;
  656. exportSpectrumDialog.Title = title;
  657. exportSpectrumDialog.FilterIndex = 1;
  658. if (exportSpectrumDialog.ShowDialog() == DialogResult.OK)
  659. {
  660. switch (exportSpectrumDialog.FilterIndex)
  661. {
  662. case 1:
  663. bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Bmp);
  664. break;
  665. case 2:
  666. bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Png);
  667. break;
  668. case 3:
  669. bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Tiff);
  670. break;
  671. case 4:
  672. bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Jpeg);
  673. break;
  674. default:
  675. bitmap.Save(exportSpectrumDialog.FileName, ImageFormat.Bmp);
  676. break;
  677. }
  678. }
  679. }
  680. }
  681. catch (ArgumentException)
  682. {
  683. }
  684. }
  685. /// <summary>
  686. /// ExitToolStripMenuItem click handler
  687. /// </summary>
  688. /// <param name="sender">sender object</param>
  689. /// <param name="e">EventArgs</param>
  690. private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
  691. {
  692. Application.Exit();
  693. }
  694. /// <summary>
  695. /// Detector Control Tool strip menu item click handler
  696. /// </summary>
  697. /// <param name="sender">sender object</param>
  698. /// <param name="e">EventArgs</param>
  699. private void DetectorControlToolStripMenuItem_Click(object sender, EventArgs e)
  700. {
  701. Form detectorControlPanel = new DetectorControlPanel(OIHelper.EdDetectorControl);
  702. detectorControlPanel.Show();
  703. }
  704. /// <summary>
  705. /// Microscope Control Tool strip menu item click handler
  706. /// </summary>
  707. /// <param name="sender">sender object</param>
  708. /// <param name="e">EventArgs</param>
  709. private void MicroscopeControlToolStripMenuItem_Click(object sender, EventArgs e)
  710. {
  711. Form microscopeControlPanel = new MicroscopeControlPanel();
  712. microscopeControlPanel.Show();
  713. }
  714. /// <summary>
  715. /// Calibrate ToolStrip MenuItem Click event handler.
  716. /// </summary>
  717. /// <param name="sender">sender</param>
  718. /// <param name="e">e</param>
  719. private void CalibrateToolStripMenuItem_Click(object sender, EventArgs e)
  720. {
  721. Form calibrate = new Calibrate();
  722. calibrate.Show();
  723. }
  724. /// <summary>
  725. /// Auto Id Settings Form
  726. /// </summary>
  727. /// <param name="sender">sender</param>
  728. /// <param name="e">EventArgs</param>
  729. private void AutoIdSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  730. {
  731. Form autoIDSettings = new AutoIdSettings();
  732. autoIDSettings.Show();
  733. }
  734. /// <summary>
  735. /// Handles the Click event of the periodicTableVisualSettingsToolStripMenuItem control.
  736. /// </summary>
  737. /// <param name="sender">The source of the event.</param>
  738. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  739. private void PeriodicTableVisualSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  740. {
  741. if (this.periodicTableVisualSettings == null || this.periodicTableVisualSettings.IsDisposed)
  742. {
  743. this.periodicTableVisualSettings = new PeriodicTableVisualSettings();
  744. }
  745. this.periodicTableVisualSettings.Show();
  746. this.periodicTableVisualSettings.BringToFront();
  747. }
  748. /// <summary>
  749. /// Handles the Click event of the elementComboVisualSettingsToolStripMenuItem control.
  750. /// </summary>
  751. /// <param name="sender">The source of the event.</param>
  752. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  753. private void ElementComboVisualSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  754. {
  755. if (this.elementComboVisualSettings == null || this.elementComboVisualSettings.IsDisposed)
  756. {
  757. this.elementComboVisualSettings = new ElementComboVisualSettings();
  758. }
  759. this.elementComboVisualSettings.Show();
  760. this.elementComboVisualSettings.BringToFront();
  761. }
  762. /// <summary>
  763. /// Handles the Click event of the SpectrumViewerVisualSettingsToolStripMenuItem control.
  764. /// </summary>
  765. /// <param name="sender">The source of the event.</param>
  766. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  767. private void SpectrumViewerVisualSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  768. {
  769. if (this.spectrumViewerVisualSettings == null || this.spectrumViewerVisualSettings.IsDisposed)
  770. {
  771. this.spectrumViewerVisualSettings = new SpectrumViewerVisualSettings();
  772. }
  773. this.spectrumViewerVisualSettings.Show();
  774. this.spectrumViewerVisualSettings.BringToFront();
  775. }
  776. /// <summary>
  777. /// Handles the Click event of the ImageViewerVisualSetting control.
  778. /// </summary>
  779. /// <param name="sender">The source of the event.</param>
  780. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  781. private void ImageViewerVisualSetting_Click(object sender, EventArgs e)
  782. {
  783. if (this.imageViewVisualSettingsForm == null || this.imageViewVisualSettingsForm.IsDisposed)
  784. {
  785. this.imageViewVisualSettingsForm = new ImageViewerVisualSettings();
  786. }
  787. this.imageViewVisualSettingsForm.Show();
  788. this.imageViewVisualSettingsForm.BringToFront();
  789. }
  790. /// <summary>
  791. /// Handles the Click event of the BrightnessContractDialogVisualSettingsToolStripMenuItem control.
  792. /// </summary>
  793. /// <param name="sender">The source of the event.</param>
  794. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  795. private void BrightnessContractDialogVisualSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  796. {
  797. if (this.brightnessContrastDialogVisualSettings == null || this.brightnessContrastDialogVisualSettings.IsDisposed)
  798. {
  799. this.brightnessContrastDialogVisualSettings = new BCDialogVisualSettings();
  800. }
  801. this.brightnessContrastDialogVisualSettings.Show();
  802. this.brightnessContrastDialogVisualSettings.BringToFront();
  803. }
  804. /// <summary>
  805. /// Called when [details visual settings menu item click].
  806. /// </summary>
  807. /// <param name="sender">The sender.</param>
  808. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  809. private void OnDetailsVisualSettingsMenuItemClick(object sender, EventArgs e)
  810. {
  811. if (this.detailsDialogVisualSettingsForm == null || this.detailsDialogVisualSettingsForm.IsDisposed)
  812. {
  813. this.detailsDialogVisualSettingsForm = new DetailDialogVisualSettings();
  814. }
  815. this.detailsDialogVisualSettingsForm.Show();
  816. this.detailsDialogVisualSettingsForm.BringToFront();
  817. }
  818. /// <summary>
  819. /// Handles the Click event of the LoadSpectrumDataToolStripMenuItem_Click.
  820. /// </summary>
  821. /// <param name="sender">The source of the event.</param>
  822. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  823. private void LoadSpectrumDataToolStripMenuItem_Click(object sender, EventArgs e)
  824. {
  825. try
  826. {
  827. using (var of_dlg = new OpenFileDialog())
  828. {
  829. of_dlg.Title = @"Select a file";
  830. of_dlg.Filter = @"Esd(*.esd)|*.esd|Iex(*.iex)|*.iex";
  831. of_dlg.FilterIndex = 1;
  832. if (of_dlg.ShowDialog() == DialogResult.OK)
  833. {
  834. if (!File.Exists(of_dlg.FileName))
  835. {
  836. throw new InvalidOperationException("File does not exists!");
  837. }
  838. this.spectrumViewer.ClearComparisonSpectra();
  839. switch (of_dlg.FilterIndex)
  840. {
  841. case 1:
  842. this.spectrumViewer.Spectrum = DataFactory.LoadEdSpectrum(OIHelper.FileToByteArray(of_dlg.FileName));
  843. break;
  844. case 2:
  845. this.spectrumViewer.Spectrum = DataFactory.LoadEdSpectrumFromIex(of_dlg.FileName);
  846. break;
  847. }
  848. // Set Periodic Table control element state
  849. this.periodicTableControl.ClearAll();
  850. this.periodicTableControl.IncludeElements(this.spectrumViewer.Spectrum.IdentifiedElements.ToArray());
  851. }
  852. }
  853. }
  854. catch (SerializationException)
  855. {
  856. }
  857. }
  858. /// <summary>
  859. /// SaveSpectrumDataToolStripMenuItem_Click
  860. /// </summary>
  861. /// <param name="sender">The source of the event.</param>
  862. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  863. private void SaveSpectrumDataToolStripMenuItem_Click(object sender, EventArgs e)
  864. {
  865. string filter = @"Esd(*.esd)|*.esd|Iex(*.iex)|*.iex";
  866. string title = @"Save spectrum as";
  867. var spectrum = this.spectrumViewer.Spectrum;
  868. if (spectrum != null)
  869. {
  870. try
  871. {
  872. using (var dialog = new SaveFileDialog())
  873. {
  874. dialog.Filter = filter;
  875. dialog.Title = title;
  876. dialog.FilterIndex = 1;
  877. if (dialog.ShowDialog() == DialogResult.OK)
  878. {
  879. switch (dialog.FilterIndex)
  880. {
  881. case 1:
  882. byte[] spectrumData = spectrum.Save();
  883. OIHelper.ByteArrayToFile(dialog.FileName, spectrumData);
  884. break;
  885. case 2:
  886. OIHelper.EdSpectrumProcessing.CreateIexSpectrum(dialog.FileName, spectrum);
  887. break;
  888. default:
  889. break;
  890. }
  891. }
  892. }
  893. }
  894. catch (ArgumentException)
  895. {
  896. }
  897. }
  898. }
  899. /// <summary>
  900. /// Handles the Click event of the LoadImageDataToolStripMenuItem_Click.
  901. /// </summary>
  902. /// <param name="sender">The source of the event.</param>
  903. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  904. private void LoadImageDataToolStripMenuItem_Click(object sender, EventArgs e)
  905. {
  906. try
  907. {
  908. using (OpenFileDialog dialog = new OpenFileDialog())
  909. {
  910. dialog.Title = @"Select a file";
  911. dialog.Filter = @"Img(*.img)|*.img";
  912. dialog.FilterIndex = 1;
  913. if (dialog.ShowDialog() == DialogResult.OK)
  914. {
  915. this.imageViewer.DisplayImage = null;
  916. if (!File.Exists(dialog.FileName))
  917. {
  918. throw new System.InvalidOperationException("File does not exists!");
  919. }
  920. this.imageViewer.DisplayImage = DataFactory.LoadElectronImage(OIHelper.FileToByteArray(dialog.FileName));
  921. }
  922. }
  923. }
  924. catch (SerializationException)
  925. {
  926. }
  927. }
  928. /// <summary>
  929. /// SaveImageDataToolStripMenuItem_Click
  930. /// </summary>
  931. /// <param name="sender">The source of the event.</param>
  932. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  933. private void SaveImageDataToolStripMenuItem_Click(object sender, EventArgs e)
  934. {
  935. string filter = @"Img(*.img)|*.img";
  936. string title = @"Save image as";
  937. if (this.imageViewer.DisplayImage != null)
  938. {
  939. try
  940. {
  941. var electronImage = this.imageViewer.DisplayImage;
  942. using (SaveFileDialog dialog = new SaveFileDialog())
  943. {
  944. dialog.Filter = filter;
  945. dialog.Title = title;
  946. dialog.FilterIndex = 1;
  947. if (dialog.ShowDialog() == DialogResult.OK)
  948. {
  949. byte[] imageData = electronImage.Save();
  950. OIHelper.ByteArrayToFile(dialog.FileName, imageData);
  951. }
  952. }
  953. }
  954. catch (ArgumentException)
  955. {
  956. }
  957. }
  958. }
  959. /// <summary>
  960. /// EDMapAcquisitionToolStripMenuItem_Click
  961. /// </summary>
  962. /// <param name="sender">The source of the event.</param>
  963. /// <param name="e">instance containing the event data.</param>
  964. private void EDMapAcquisitionToolStripMenuItem_Click(object sender, EventArgs e)
  965. {
  966. #if ENABLE_MAP_ACQUIRE
  967. EdMapAcquisition edMapAcquisition = new EdMapAcquisition();
  968. edMapAcquisition.Show();
  969. #endif
  970. }
  971. #endregion
  972. #region Control Spectrum Processing
  973. #region Control AutoID
  974. /// <summary>
  975. /// Add element to excluded list
  976. /// </summary>
  977. /// <param name="sender">sender object</param>
  978. /// <param name="e">EventArgs</param>
  979. private void AddExcludeButton_Click(object sender, EventArgs e)
  980. {
  981. int element = this.excludeElementCombo.CurrentElement;
  982. try
  983. {
  984. this.periodicTableControl.ExcludeElements(element);
  985. }
  986. catch (FormatException fe)
  987. {
  988. this.tbExcludedElementList.Text = fe.Message;
  989. }
  990. }
  991. /// <summary>
  992. /// Remove element from excluded list
  993. /// </summary>
  994. /// <param name="sender">sender object</param>
  995. /// <param name="e">EventArgs</param>
  996. private void DeleteExcludeButton_Click(object sender, EventArgs e)
  997. {
  998. int element = this.excludeElementCombo.CurrentElement;
  999. try
  1000. {
  1001. this.periodicTableControl.Clear(element);
  1002. }
  1003. catch (FormatException fe)
  1004. {
  1005. this.tbExcludedElementList.Text = fe.Message;
  1006. }
  1007. }
  1008. /// <summary>
  1009. /// Do AutoID Button Click Handler
  1010. /// </summary>
  1011. /// <param name="sender">sender object</param>
  1012. /// <param name="e">EventArgs</param>
  1013. private void DoAutoIDButton_Click(object sender, EventArgs e)
  1014. {
  1015. this.DoAutoID();
  1016. }
  1017. /// <summary>
  1018. /// Do AutoID
  1019. /// </summary>
  1020. private void DoAutoID()
  1021. {
  1022. var spectrum = this.spectrumViewer.Spectrum;
  1023. if (spectrum != null)
  1024. {
  1025. this.tbIdentifiedElement.Clear();
  1026. IEnumerable<int> elementList = OIHelper.EdSpectrumProcessing.IdentifyElements(
  1027. spectrum,
  1028. OIHelper.AutoIdSettings);
  1029. foreach (int i in elementList)
  1030. {
  1031. this.tbIdentifiedElement.Text = this.tbIdentifiedElement.Text +
  1032. ElementProperties.GetElementSymbol(i) +
  1033. Environment.NewLine;
  1034. }
  1035. // Update spectrum peak label
  1036. this.UpdateSpectrumPeakLabels();
  1037. }
  1038. }
  1039. /// <summary>
  1040. /// Update current spectrum peak labels
  1041. /// </summary>
  1042. private void UpdateSpectrumPeakLabels()
  1043. {
  1044. try
  1045. {
  1046. OIHelper.EdSpectrumProcessing.UpdatePeakLabels(this.spectrumViewer.Spectrum, null);
  1047. }
  1048. catch (ArgumentNullException)
  1049. {
  1050. }
  1051. catch (InvalidOperationException)
  1052. {
  1053. }
  1054. }
  1055. #endregion
  1056. #region Control Quantitative Analysis
  1057. /// <summary>
  1058. /// Do quant button click handler
  1059. /// </summary>
  1060. /// <param name="sender">sender object</param>
  1061. /// <param name="e">EventArgs</param>
  1062. private void DoQuantButton_Click(object sender, EventArgs e)
  1063. {
  1064. this.DoQuant();
  1065. }
  1066. /// <summary>
  1067. /// Do quantitative processing and display
  1068. /// </summary>
  1069. private void DoQuant()
  1070. {
  1071. switch (OIHelper.SEMQuantSettings.ProcessingOption)
  1072. {
  1073. case ProcessingOption.ElementByDifference:
  1074. OIHelper.SEMQuantSettings.CombinedElement = int.Parse(this.tbCombinedElement.Text, CultureInfo.InvariantCulture);
  1075. break;
  1076. case ProcessingOption.ElementByStoichiometry:
  1077. // While it is possible to choose other elements, Oxygen is the only supported element by stoichiometry.
  1078. OIHelper.SEMQuantSettings.CombinedElement = 8;
  1079. OIHelper.SEMQuantSettings.TypeOfIon = IonType.Anion;
  1080. OIHelper.SEMQuantSettings.NumberOfIons = double.Parse(this.tbNumberOfIons.Text, CultureInfo.InvariantCulture);
  1081. break;
  1082. case ProcessingOption.AllElements:
  1083. default:
  1084. break;
  1085. }
  1086. if (this.cbIsCoated.Checked)
  1087. {
  1088. OIHelper.SEMQuantSettings.SampleCoating.IsCoated = true;
  1089. OIHelper.SEMQuantSettings.SampleCoating.Thickness = double.Parse(this.tbThickness.Text, CultureInfo.InvariantCulture);
  1090. OIHelper.SEMQuantSettings.SampleCoating.Density = double.Parse(this.tbDensity.Text, CultureInfo.InvariantCulture);
  1091. OIHelper.SEMQuantSettings.SampleCoating.CoatingElement = int.Parse(this.tbCoatingElement.Text, CultureInfo.InvariantCulture);
  1092. }
  1093. else
  1094. {
  1095. OIHelper.SEMQuantSettings.SampleCoating.IsCoated = false;
  1096. }
  1097. if (this.cbThresholding.Checked)
  1098. {
  1099. OIHelper.SEMQuantSettings.Thresholding = true;
  1100. OIHelper.SEMQuantSettings.SigmaLevel = double.Parse(this.tbSigmaLevel.Text, CultureInfo.InvariantCulture);
  1101. }
  1102. else
  1103. {
  1104. OIHelper.SEMQuantSettings.Thresholding = false;
  1105. }
  1106. OIHelper.SEMQuantSettings.Normalised = this.cbNormalised.Checked;
  1107. this.dgQuantResult.Rows.Clear();
  1108. try
  1109. {
  1110. ISEMQuantStatus quantStatus = OIHelper.EdSpectrumProcessing.SEMQuantifySpectrum(this.spectrumViewer.Spectrum, OIHelper.SEMQuantSettings);
  1111. this.tbQuantStatusMessage.Text = quantStatus.Status.ToString() + System.Environment.NewLine +
  1112. quantStatus.StatusMessage;
  1113. double totalWeightPercent = 0;
  1114. double totalAtomicPercent = 0;
  1115. foreach (ISEMQuantResult result in quantStatus.Results)
  1116. {
  1117. if (result.WeightPercent != 0)
  1118. {
  1119. this.dgQuantResult.Rows.Add(new object[]
  1120. {
  1121. ElementProperties.GetElementSymbol(result.AtomicNumber), result.LineType,
  1122. Math.Round(result.WeightPercent, 2), Math.Round(result.WeightPercentSigma, 6),
  1123. Math.Round(result.AtomicPercent, 2)
  1124. });
  1125. }
  1126. totalWeightPercent += result.WeightPercent;
  1127. totalAtomicPercent += result.AtomicPercent;
  1128. }
  1129. this.dgQuantResult.Rows.Add(new object[]
  1130. {
  1131. @"Total", null, Math.Round(totalWeightPercent, 2), null, Math.Round(totalAtomicPercent, 2)
  1132. });
  1133. this.spectrumViewer.SEMQuantify(OIHelper.SEMQuantSettings);
  1134. }
  1135. catch (QuantificationException ex)
  1136. {
  1137. MessageBox.Show(ex.Message, @"Quantification Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  1138. }
  1139. catch (ArgumentNullException)
  1140. {
  1141. }
  1142. }
  1143. /// <summary>
  1144. /// The last element set
  1145. /// </summary>
  1146. private int lastElementSet = -1;
  1147. /// <summary>
  1148. /// ExcludeElementCombo_PropertyChanged
  1149. /// </summary>
  1150. /// <param name="sender">sender object</param>
  1151. /// <param name="e">PropertyChangedEventArgs</param>
  1152. private void ExcludeElementCombo_PropertyChanged(object sender, PropertyChangedEventArgs e)
  1153. {
  1154. if (e.PropertyName == @"CurrentElement")
  1155. {
  1156. this.UpdateHighlightedElementInTable();
  1157. }
  1158. }
  1159. /// <summary>
  1160. /// Updates the highlighted element in table.
  1161. /// </summary>
  1162. private void UpdateHighlightedElementInTable()
  1163. {
  1164. if (this.lastElementSet != -1)
  1165. {
  1166. this.periodicTableControl.SetElementColor(this.lastElementSet, System.Windows.Media.Colors.Transparent);
  1167. }
  1168. System.Windows.Media.Color currentElementColor = System.Windows.Media.Color.FromArgb(255, 200, 255, 0);
  1169. this.periodicTableControl.SetElementColor(this.excludeElementCombo.CurrentElement, currentElementColor);
  1170. this.periodicTableControl.ShowElementColors = true;
  1171. this.lastElementSet = this.excludeElementCombo.CurrentElement;
  1172. }
  1173. /// <summary>
  1174. /// Add deconvolution element to list
  1175. /// </summary>
  1176. /// <param name="sender">sender object</param>
  1177. /// <param name="e">EventArgs</param>
  1178. private void AddDeconvolutionButton_Click(object sender, EventArgs e)
  1179. {
  1180. this.tbDeconvolutionElementList.Clear();
  1181. int element = this.deconvolutionElementCombo.CurrentElement;
  1182. try
  1183. {
  1184. OIHelper.SEMQuantSettings.SetDeconvolutionElement(element, true);
  1185. foreach (int i in OIHelper.SEMQuantSettings.DeconvolutionElements)
  1186. {
  1187. this.tbDeconvolutionElementList.Text += i.ToString(CultureInfo.InvariantCulture) + System.Environment.NewLine;
  1188. }
  1189. }
  1190. catch (FormatException fe)
  1191. {
  1192. this.tbDeconvolutionElementList.Text = fe.Message;
  1193. }
  1194. catch (ArgumentOutOfRangeException ae)
  1195. {
  1196. this.tbDeconvolutionElementList.Text = ae.Message;
  1197. }
  1198. }
  1199. /// <summary>
  1200. /// Remove element from deconvolution list
  1201. /// </summary>
  1202. /// <param name="sender">sender object</param>
  1203. /// <param name="e">EventArgs</param>
  1204. private void DeleteDeconvolutionButton_Click(object sender, EventArgs e)
  1205. {
  1206. this.tbDeconvolutionElementList.Clear();
  1207. int element = this.deconvolutionElementCombo.CurrentElement;
  1208. try
  1209. {
  1210. OIHelper.SEMQuantSettings.SetDeconvolutionElement(element, false);
  1211. foreach (int i in OIHelper.SEMQuantSettings.DeconvolutionElements)
  1212. {
  1213. this.tbDeconvolutionElementList.Text += i.ToString(CultureInfo.InvariantCulture) + System.Environment.NewLine;
  1214. }
  1215. }
  1216. catch (FormatException fe)
  1217. {
  1218. this.tbDeconvolutionElementList.Text = fe.Message;
  1219. }
  1220. }
  1221. /// <summary>
  1222. /// Clear deconvolution elements
  1223. /// </summary>
  1224. /// <param name="sender">sender object</param>
  1225. /// <param name="e">EventArgs</param>
  1226. private void ClearListButton_Click(object sender, EventArgs e)
  1227. {
  1228. this.tbDeconvolutionElementList.Clear();
  1229. OIHelper.SEMQuantSettings.ClearDeconvolutionElements();
  1230. foreach (int i in OIHelper.SEMQuantSettings.DeconvolutionElements)
  1231. {
  1232. this.tbDeconvolutionElementList.Text += i.ToString(CultureInfo.InvariantCulture) + System.Environment.NewLine;
  1233. }
  1234. }
  1235. /// <summary>
  1236. /// Used when processing spectra from specimens in which all elements yield X-rays which can be readily detected.
  1237. /// </summary>
  1238. /// <param name="sender">sender object</param>
  1239. /// <param name="e">EventArgs</param>
  1240. private void AllElementsRadiobutton_Click(object sender, EventArgs e)
  1241. {
  1242. OIHelper.SEMQuantSettings.ProcessingOption = ProcessingOption.AllElements;
  1243. this.cbNormalised.Enabled = true;
  1244. this.tbCombinedElement.Enabled = false;
  1245. this.tbNumberOfIons.Enabled = false;
  1246. }
  1247. /// <summary>
  1248. /// Calculated assuming that the difference between the analyzed total and 100%
  1249. /// </summary>
  1250. /// <param name="sender">sender object</param>
  1251. /// <param name="e">EventArgs</param>
  1252. private void ElementByDifferenceRadioButton_Click(object sender, EventArgs e)
  1253. {
  1254. OIHelper.SEMQuantSettings.ProcessingOption = ProcessingOption.ElementByDifference;
  1255. this.cbNormalised.Checked = false;
  1256. this.cbNormalised.Enabled = false;
  1257. this.tbCombinedElement.Enabled = true;
  1258. this.tbNumberOfIons.Enabled = false;
  1259. }
  1260. /// <summary>
  1261. /// Concentration of oxygen to be calculated.
  1262. /// </summary>
  1263. /// <param name="sender">sender object</param>
  1264. /// <param name="e">EventArgs</param>
  1265. private void ElementByStoichiometryRadiobutton_Click(object sender, EventArgs e)
  1266. {
  1267. OIHelper.SEMQuantSettings.ProcessingOption = ProcessingOption.ElementByStoichiometry;
  1268. this.cbNormalised.Enabled = true;
  1269. this.tbCombinedElement.Enabled = false;
  1270. this.tbNumberOfIons.Enabled = true;
  1271. }
  1272. /// <summary>
  1273. /// ThresholdingCheckbox checked
  1274. /// </summary>
  1275. /// <param name="sender">sender object</param>
  1276. /// <param name="e">EventArgs</param>
  1277. private void ThresholdingCheckbox_CheckedChanged(object sender, EventArgs e)
  1278. {
  1279. this.tbSigmaLevel.Enabled = this.cbThresholding.Checked;
  1280. }
  1281. /// <summary>
  1282. /// IsCoatedCheckbox checked
  1283. /// </summary>
  1284. /// <param name="sender">sender object</param>
  1285. /// <param name="e">EventArgs</param>
  1286. private void IsCoatedCheckbox_CheckedChanged(object sender, EventArgs e)
  1287. {
  1288. this.tbCoatingElement.Enabled = this.cbIsCoated.Checked;
  1289. this.tbDensity.Enabled = this.cbIsCoated.Checked;
  1290. this.tbThickness.Enabled = this.cbIsCoated.Checked;
  1291. }
  1292. #endregion
  1293. #endregion
  1294. #region Periodic Table
  1295. /// <summary>
  1296. /// Handles the CollectionChanged event of the PeriodicTableExcludedElements control.
  1297. /// </summary>
  1298. /// <param name="sender">The source of the event.</param>
  1299. /// <param name="e">The <see cref="NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
  1300. private void PeriodicTableExcludedElements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  1301. {
  1302. this.UpdateElementsList(this.periodicTableControl.ExcludedElements, this.tbExcludedElementList);
  1303. }
  1304. /// <summary>
  1305. /// Handles the CollectionChanged event of the PeriodicTableIncludedElements control.
  1306. /// </summary>
  1307. /// <param name="sender">The source of the event.</param>
  1308. /// <param name="e">The <see cref="NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
  1309. private void PeriodicTableIncludedElements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  1310. {
  1311. this.UpdateElementsList(this.periodicTableControl.IncludedElements, this.tbIncludedElementList);
  1312. var spectrum = this.spectrumViewer.Spectrum;
  1313. if (spectrum != null)
  1314. {
  1315. switch (e.Action)
  1316. {
  1317. case NotifyCollectionChangedAction.Add:
  1318. foreach (int i in e.NewItems.Cast<int>())
  1319. {
  1320. spectrum.SetIdentifiedElement(i, true);
  1321. }
  1322. break;
  1323. case NotifyCollectionChangedAction.Remove:
  1324. foreach (int i in e.OldItems.Cast<int>())
  1325. {
  1326. spectrum.SetIdentifiedElement(i, false);
  1327. }
  1328. break;
  1329. default:
  1330. break;
  1331. }
  1332. this.UpdateSpectrumPeakLabels();
  1333. }
  1334. }
  1335. /// <summary>
  1336. /// Handles the CollectionChanged event of the PeriodicTableUnsetElements control.
  1337. /// </summary>
  1338. /// <param name="sender">The source of the event.</param>
  1339. /// <param name="e">The <see cref="NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
  1340. private void PeriodicTableUnsetElements_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  1341. {
  1342. this.UpdateElementsList(this.periodicTableControl.UnsetElements, this.tbUnsetElementList);
  1343. }
  1344. /// <summary>
  1345. /// Handles the CheckedChanged event of the EnableMultiselectToolStripMenuItem control.
  1346. /// </summary>
  1347. /// <param name="sender">The source of the event.</param>
  1348. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  1349. private void EnableMultiselectToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
  1350. {
  1351. this.periodicTableControl.SelectionMode = this.enableMultiselectToolStripMenuItem.Checked ?
  1352. System.Windows.Controls.SelectionMode.Multiple : System.Windows.Controls.SelectionMode.Single;
  1353. }
  1354. /// <summary>
  1355. /// Handles the SelectedIndexChanged event of the PeriodicTableBackgroundToolStripComboBox control.
  1356. /// </summary>
  1357. /// <param name="sender">The source of the event.</param>
  1358. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  1359. private void PeriodicTableBackgroundToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
  1360. {
  1361. if (this.periodicTableBackgroundToolStripComboBox.SelectedItem != null)
  1362. {
  1363. System.Drawing.Color selectedColor = System.Drawing.Color.FromKnownColor((System.Drawing.KnownColor)this.periodicTableBackgroundToolStripComboBox.SelectedItem);
  1364. this.periodicTableElementHost.BackColor = selectedColor;
  1365. this.periodicTableBackgroundToolStripComboBox.BackColor = selectedColor;
  1366. this.periodicTableBackgroundToolStripComboBox.ForeColor = selectedColor.ToArgb() > (0xFFFFFFFF / 2) ? System.Drawing.Color.Black : System.Drawing.Color.LightGray;
  1367. }
  1368. }
  1369. /// <summary>
  1370. /// Initializes the enabled elements menu items.
  1371. /// </summary>
  1372. private void InitializeEnabledElementsMenuItems()
  1373. {
  1374. ToolStripMenuItem item;
  1375. for (int element = 1; element <= 103; element++)
  1376. {
  1377. IPeriodicTableElement info = this.periodicTableControl.LookupElementInfo(element);
  1378. if (info != null)
  1379. {
  1380. item = new ToolStripMenuItem(info.ElementName);
  1381. item.CheckOnClick = true;
  1382. item.Checked = true;
  1383. item.Tag = element;
  1384. item.CheckedChanged += this.ElementEnabled_CheckedChanged;
  1385. this.enabledElementsMenuItem.DropDownItems.Add(item);
  1386. }
  1387. }
  1388. }
  1389. /// <summary>
  1390. /// Handles the CheckedChanged event of the ElementEnabled control.
  1391. /// </summary>
  1392. /// <param name="sender">The source of the event.</param>
  1393. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  1394. private void ElementEnabled_CheckedChanged(object sender, EventArgs e)
  1395. {
  1396. ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
  1397. if (menuItem != null)
  1398. {
  1399. int elementNo = (int)menuItem.Tag;
  1400. this.periodicTableControl.EnableElements(menuItem.Checked, elementNo);
  1401. }
  1402. }
  1403. #endregion
  1404. /// <summary>
  1405. /// Handles the SelectedIndexChanged event of the CbCompareColor control.
  1406. /// </summary>
  1407. /// <param name="sender">The source of the event.</param>
  1408. /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
  1409. private void CbCompareColor_SelectedIndexChanged(object sender, EventArgs e)
  1410. {
  1411. this.addCompareSpectraButton.BackColor = Color.FromKnownColor((KnownColor)this.cbCompareColor.SelectedItem);
  1412. }
  1413. /// <summary>
  1414. /// Called when [image tool ComboBox selected index changed].
  1415. /// </summary>
  1416. /// <param name="sender">The sender.</param>
  1417. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  1418. private void OnImageToolComboBoxSelectedIndexChanged(object sender, EventArgs e)
  1419. {
  1420. if (this.imageViewer != null)
  1421. {
  1422. this.imageViewer.InteractionTool = (ImageInteractionTool)this.imageToolComboBox.SelectedItem;
  1423. }
  1424. }
  1425. /// <summary>
  1426. /// Called when [spectrum tool ComboBox selected index changed].
  1427. /// </summary>
  1428. /// <param name="sender">The sender.</param>
  1429. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  1430. private void OnSpectrumToolComboBoxSelectedIndexChanged(object sender, EventArgs e)
  1431. {
  1432. if (this.spectrumViewer != null)
  1433. {
  1434. this.spectrumViewer.InteractionTool = (InteractionTool)this.spectrumInteractionToolComboBox.SelectedItem;
  1435. }
  1436. }
  1437. /// <summary>
  1438. /// Adds a spectrum for comparison.
  1439. /// </summary>
  1440. /// <param name="sender">The sender.</param>
  1441. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  1442. private void OnAddCompareSpectraButtonClick(object sender, EventArgs e)
  1443. {
  1444. var spectrum = this.cbCompareSpectraComboBox.SelectedItem as IEdSpectrum;
  1445. if (spectrum != null)
  1446. {
  1447. // Add Current Spectrum to Compare source
  1448. WindowsMedia.Color spectrumColor = ColorConverter.Convert(this.addCompareSpectraButton.BackColor);
  1449. this.spectrumViewer.AddComparisonSpectrum(spectrum, spectrumColor);
  1450. this.cbCompareColor.SelectedItem = GetRandomColor();
  1451. }
  1452. }
  1453. /// <summary>
  1454. /// Loads the comparison spectra.
  1455. /// </summary>
  1456. private void LoadComparisonSpectra()
  1457. {
  1458. byte[] data;
  1459. this.comparisonSpectra = new List<IEdSpectrum>();
  1460. using (FileStream file = new FileStream(@"ComparisonSpectra\Nickel.spec", FileMode.Open))
  1461. {
  1462. data = new byte[file.Length];
  1463. file.Read(data, 0, (int)file.Length);
  1464. this.comparisonSpectra.Add(DataFactory.LoadEdSpectrum(data));
  1465. }
  1466. using (FileStream file = new FileStream(@"ComparisonSpectra\Preview spectrum.spec", FileMode.Open))
  1467. {
  1468. data = new byte[file.Length];
  1469. file.Read(data, 0, (int)file.Length);
  1470. this.comparisonSpectra.Add(DataFactory.LoadEdSpectrum(data));
  1471. }
  1472. using (FileStream file = new FileStream(@"ComparisonSpectra\Tungsten particle.spec", FileMode.Open))
  1473. {
  1474. data = new byte[file.Length];
  1475. file.Read(data, 0, (int)file.Length);
  1476. this.comparisonSpectra.Add(DataFactory.LoadEdSpectrum(data));
  1477. }
  1478. this.cbCompareSpectraComboBox.DataSource = this.comparisonSpectra;
  1479. this.cbCompareSpectraComboBox.DisplayMember = @"Label";
  1480. this.cbCompareSpectraComboBox.SelectedIndex = 0;
  1481. }
  1482. /// <summary>
  1483. /// Called when [clear excluded list button clicked].
  1484. /// </summary>
  1485. /// <param name="sender">The sender.</param>
  1486. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  1487. private void OnClearExcludedListButtonClicked(object sender, EventArgs e)
  1488. {
  1489. foreach (int element in this.periodicTableControl.ExcludedElements)
  1490. {
  1491. this.periodicTableControl.Clear(element);
  1492. }
  1493. this.UpdateElementsList(this.periodicTableControl.ExcludedElements, this.tbExcludedElementList);
  1494. }
  1495. /// <summary>
  1496. /// Called when [export tiff button clicked].
  1497. /// </summary>
  1498. /// <param name="sender">The sender.</param>
  1499. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  1500. private void OnExportTiffButtonClicked(object sender, EventArgs e)
  1501. {
  1502. // Export a tiff synchronously on the calling thread.
  1503. var syncTiff = this.spectrumViewer.CreateImage(ExportImageType.Tiff);
  1504. File.WriteAllBytes(Path.Combine(Path.GetTempPath(), @"zSpectrumSync.tiff"), syncTiff);
  1505. // Export a tiff asynchronously on a background thread.
  1506. // The thread must be created with an apartment state set to STA.
  1507. // Calls to CreateImage() must be made on the same thread that created the viewer.
  1508. var spectrum = this.spectrumViewer.Spectrum;
  1509. var thread = new System.Threading.Thread(() =>
  1510. {
  1511. // For optimum performance, create a single spectrum viewer and change the spectrum
  1512. // and viewer settings where required for all spectra that you need images for.
  1513. var backgroundViewer = new SpectrumViewer();
  1514. // A height and width must be set to provide the image dimensions in pixels.
  1515. backgroundViewer.Width = 500;
  1516. backgroundViewer.Height = 500;
  1517. backgroundViewer.IsYAxisAutoScaleEnabled = true;
  1518. backgroundViewer.YAxisType = ScaleType.Logarithmic;
  1519. backgroundViewer.MaximumEnergy = 8;
  1520. backgroundViewer.Spectrum = spectrum;
  1521. var asyncTiff = backgroundViewer.CreateImage(ExportImageType.Tiff);
  1522. File.WriteAllBytes(Path.Combine(Path.GetTempPath(), @"zSpectrumAsync.tiff"), asyncTiff);
  1523. backgroundViewer.Dispatcher.InvokeShutdown();
  1524. });
  1525. thread.IsBackground = true;
  1526. thread.SetApartmentState(ApartmentState.STA);
  1527. thread.Start();
  1528. }
  1529. }
  1530. }