Ejemplo n.º 1
0
        void SaveDoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = false;
            _statusVM.Busy++;
            DateTime timestamp = DateTime.Now;
            var      sts       = new StatusMessage {
                Timestamp = timestamp, Message = "Saving spectrometer..."
            };

            _statusVM.SpecMessages.Add(sts);
            _timestamps.Push(timestamp);

            try
            {
                List <Point> spect = (List <Point>)(e.Argument);

                if (spect == null || spect.Count == 0)
                {
                    throw new Exception("Bad data");
                }

                double[] spectrumData   = spect.Select(p => p.Y).ToArray();;
                double[] wavelengthData = spect.Select(p => p.X).ToArray();

                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.Filter = "SPC file (*.spc)|*.spc|CSV file (*.csv)|*.csv";
                if (saveDlg.ShowDialog() == true)
                {
                    if (Path.GetExtension(saveDlg.FileName).ToUpper().Contains("CSV"))
                    {
                        e.Result = SPCHelper.SaveToTXT(wavelengthData,
                                                       spectrumData,
                                                       saveDlg.FileName, "Wavelength (nm)", "Counts");
                    }
                    else
                    {
                        e.Result = SPCHelper.SaveToSPC(wavelengthData,
                                                       spectrumData,
                                                       saveDlg.FileName, "Wavelength (nm)", "Counts");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("spectrometer exception: " + ex.Message);
                e.Result = false;
            }
            finally
            {
            }
        }
Ejemplo n.º 2
0
        void SaveDoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = false;

            try
            {
                var    ts = DateTime.Now.ToString("MMddyyyy_HHmmss");
                string imageFolderPath = SaveFolderPath + @"\images_" + ts;
                Directory.CreateDirectory(imageFolderPath);
                string spectraFolderPath = SaveFolderPath + @"\spectra_" + ts;
                Directory.CreateDirectory(spectraFolderPath);

                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.InitialDirectory = imageFolderPath;
                saveDlg.Filter           = "JPG file (*.jpg)|*.jpg|BMP file (*.bmp)|*.bmp";

                if (saveDlg.ShowDialog() == true)
                {
                    string filename_without_ext = Path.GetFileNameWithoutExtension(saveDlg.FileName);
                    _cameraVM.SetSettingFilePath(imageFolderPath + "\\" + filename_without_ext);
                    _cameraVM.SetSettingCaptureTime(DateTime.Now.ToString());
                    _cameraVM.ContinueSaveSettings();

                    if ((bool)(e.Argument) == true)
                    {
                        for (int i = 0; i < _phosImages.Count; i++)
                        {
                            var fileName = imageFolderPath + @"\\" + filename_without_ext + "_" + _phosImages[i].FrameId +
                                           "_" + (Math.Round((_phosImages[i].TimeStamp - _startTimeStamp) / 1000000d, 0)) + "ms.jpg";
                            var fileNameFiltered = imageFolderPath + @"\\" + filename_without_ext + "_filtered" + "_" + _phosImages[i].FrameId +
                                                   "_" + (Math.Round((_phosImages[i].TimeStamp - _startTimeStamp) / 1000000d, 0)) + "ms.jpg";

                            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(_phosImages[i].Image))
                            {
                                if (Path.GetExtension(fileName).ToUpper().Contains("JPG"))
                                {
                                    bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                                else
                                {
                                    bmp.Save(fileName);
                                }
                            }
                            if (_phosFilteredImages.Count > 0)
                            {
                                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(_phosFilteredImages[i]))
                                {
                                    if (Path.GetExtension(fileNameFiltered).ToUpper().Contains("JPG"))
                                    {
                                        bmp.Save(fileNameFiltered, System.Drawing.Imaging.ImageFormat.Jpeg);
                                    }
                                    else
                                    {
                                        bmp.Save(fileNameFiltered);
                                    }
                                }
                            }
                        }
                        for (int i = 0; i < _phosSpectra.Count; i++)
                        {
                            var spectraFileName = spectraFolderPath + @"\\" + filename_without_ext + "_" +
                                                  _phosSpectra[i].Id +
                                                  "_" + (Math.Round((_phosSpectra[i].TimeStamp - _spectrumStartTimeStamp) / 1000d, 0)) + "ms.spc";
                            double[] wl     = _phosSpectra[i].Spectrum.Select(p => p.X).ToArray();
                            double[] counts = _phosSpectra[i].Spectrum.Select(p => p.Y).ToArray();

                            if (!SPCHelper.SaveToSPC(wl, counts, spectraFileName, "Wavelength (nm)", "Intensity"))
                            {
                                throw new Exception("spectra save fail");
                            }
                        }
                        _dataSaved = true;
                    }
                    else
                    {
                        if (_phosImages.Count > 0)
                        {
                            var imgFileName = imageFolderPath + @"\\" + filename_without_ext + "_" + _phosImages[(int)(CurrentPhosItem - 1)].FrameId +
                                              "_" + (Math.Round((_phosImages[(int)(CurrentPhosItem - 1)].TimeStamp - _startTimeStamp) / 1000000d, 0)) + "ms.jpg";
                            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(_phosImages[(int)(CurrentPhosItem - 1)].Image))
                            {
                                if (Path.GetExtension(imgFileName).ToUpper().Contains("JPG"))
                                {
                                    bmp.Save(imgFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                                else
                                {
                                    bmp.Save(imgFileName);
                                }
                            }
                        }
                        if (_phosFilteredImages.Count > 0)
                        {
                            var imgFileNameFiltered = imageFolderPath + @"\\" + filename_without_ext + "_filtered" + "_" + _phosImages[(int)(CurrentPhosItem - 1)].FrameId +
                                                      "_" + (Math.Round((_phosImages[(int)(CurrentPhosItem - 1)].TimeStamp - _startTimeStamp) / 1000000d, 0)) + "ms.jpg";
                            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(_phosFilteredImages[(int)(CurrentPhosItem - 1)]))
                            {
                                if (Path.GetExtension(imgFileNameFiltered).ToUpper().Contains("JPG"))
                                {
                                    bmp.Save(imgFileNameFiltered, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                                else
                                {
                                    bmp.Save(imgFileNameFiltered);
                                }
                            }
                        }

                        if (_phosSpectra.Count > 0)
                        {
                            var spectraFileName = spectraFolderPath + @"\\" + filename_without_ext + "_" +
                                                  _phosSpectra[(int)(CurrentSpectraItem - 1)].Id +
                                                  "_" + (Math.Round((_phosSpectra[(int)(CurrentSpectraItem - 1)].TimeStamp - _spectrumStartTimeStamp) / 1000d, 0)) + "ms.spc";
                            double[] wl     = _phosSpectra[(int)(CurrentSpectraItem - 1)].Spectrum.Select(p => p.X).ToArray();
                            double[] counts = _phosSpectra[(int)(CurrentSpectraItem - 1)].Spectrum.Select(p => p.Y).ToArray();

                            if (!SPCHelper.SaveToSPC(wl, counts, spectraFileName, "Wavelength (nm)", "Intensity"))
                            {
                                throw new Exception("spectra save fail");
                            }
                        }
                    }

                    e.Result = true;
                }
            }
            catch (Exception ex)
            {
                e.Result = false;
            }
            finally
            {
            }
        }
Ejemplo n.º 3
0
        void SaveDoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = false;

            try
            {
                long   timestamp          = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                string tempSaveFolderPath = SaveFolderPath + @"\" + timestamp.ToString();
                Directory.CreateDirectory(tempSaveFolderPath);

                string summaryFileName = tempSaveFolderPath + @"\" + "summary.bmp";

                Action a = () =>
                {
                    SaveUsingEncoder(summaryFileName, ((SaveArgs)(e.Argument)).uiElement, new PngBitmapEncoder());
                };

                App.Current.Dispatcher.Invoke(a);

                if (((SaveArgs)(e.Argument)).all == true)
                {
                    for (int m = 0; m < CamResList.Count; m++)
                    {
                        string dirRoot = tempSaveFolderPath + @"\" + m.ToString();
                        Directory.CreateDirectory(dirRoot);

                        var    ts = DateTime.Now.ToString("MMddyyyy_HHmmss");
                        string imageFolderPath = dirRoot + @"\images_" + ts;
                        Directory.CreateDirectory(imageFolderPath);
                        string spectraFolderPath = dirRoot + @"\spectra_" + ts;
                        Directory.CreateDirectory(spectraFolderPath);
                        string markerFolderPath = dirRoot + @"\markers_" + ts;
                        Directory.CreateDirectory(markerFolderPath);

                        var _phosImages = CamResList[m].Images;

                        for (int i = 0; i < _phosImages.Count; i++)
                        {
                            var fileName = imageFolderPath + @"\phos_image" + "_" + i.ToString() +
                                           "_" + _phosImages[i].TimeStamp + "ms.jpg";
                            var fileNameFiltered = imageFolderPath + @"\phos_image_filtered" + "_" + i.ToString() +
                                                   "_" + _phosImages[i].TimeStamp + "ms.jpg";

                            using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(_phosImages[i].Image))
                            {
                                if (Path.GetExtension(fileName).ToUpper().Contains("JPG"))
                                {
                                    bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                                else
                                {
                                    bmp.Save(fileName);
                                }
                            }
                            if (_phosFilteredImages?.Count > 0)
                            {
                                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(_phosFilteredImages[i]))
                                {
                                    if (Path.GetExtension(fileNameFiltered).ToUpper().Contains("JPG"))
                                    {
                                        bmp.Save(fileNameFiltered, System.Drawing.Imaging.ImageFormat.Jpeg);
                                    }
                                    else
                                    {
                                        bmp.Save(fileNameFiltered);
                                    }
                                }
                            }
                        }

                        var _phosSpectra = SpecResList[m].Spectra;
                        for (int i = 0; i < _phosSpectra.Count; i++)
                        {
                            var spectraFileName = spectraFolderPath + @"\phos_spectrum" + "_" + i.ToString() +
                                                  "_" + _phosSpectra[i].IntegrationTime.ToString() + "ms.spc";
                            double[] wl     = _phosSpectra[i].Spectrum.Select(p => p.X).ToArray();
                            double[] counts = _phosSpectra[i].Spectrum.Select(p => p.Y).ToArray();

                            if (!SPCHelper.SaveToSPC(wl, counts, spectraFileName, "Wavelength (nm)", "Intensity"))
                            {
                                throw new Exception("spectra save fail");
                            }
                        }

                        // save markers and image
                        var markerFileName = markerFolderPath + @"\pre_image" + "_" + CamResultsPre[0].TimeStamp.ToString() + "ms.jpg";
                        using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(CamResultsPre[0].Image))
                        {
                            if (Path.GetExtension(markerFileName).ToUpper().Contains("JPG"))
                            {
                                bmp.Save(markerFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                            else
                            {
                                bmp.Save(markerFileName);
                            }
                        }
                        var        markerListName = markerFolderPath + @"\markers" + ".txt";
                        TextWriter tw             = new StreamWriter(markerListName);
                        //for (int i = 0; i < MarkerList.Count; i++)
                        //{
                        tw.WriteLine(MarkerList[m + 1].Item3.ToString());
                        //}
                        tw.Close();
                    }
                    _dataSaved = true;
                }
                else
                {
                    var    ts = DateTime.Now.ToString("MMddyyyy_HHmmss");
                    string imageFolderPath = tempSaveFolderPath + @"\images_" + ts;
                    Directory.CreateDirectory(imageFolderPath);
                    string spectraFolderPath = tempSaveFolderPath + @"\spectra_" + ts;
                    Directory.CreateDirectory(spectraFolderPath);
                    string markerFolderPath = tempSaveFolderPath + @"\markers_" + ts;
                    Directory.CreateDirectory(markerFolderPath);

                    if (_phosImages.Count > 0)
                    {
                        var imgFileName = imageFolderPath + @"\phos_image" + _phosImages[(int)(CurrentPhosItem - 1)].FrameId +
                                          "_" + (Math.Round((_phosImages[(int)(CurrentPhosItem - 1)].TimeStamp - _startTimeStamp) / 1000000d, 0)) + "ms.jpg";
                        using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(_phosImages[(int)(CurrentPhosItem - 1)].Image))
                        {
                            if (Path.GetExtension(imgFileName).ToUpper().Contains("JPG"))
                            {
                                bmp.Save(imgFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                            }
                            else
                            {
                                bmp.Save(imgFileName);
                            }
                        }
                    }

                    if (_phosSpectra.Count > 0)
                    {
                        var spectraFileName = spectraFolderPath + @"\phos_spectrum" +
                                              "_" + _phosSpectra[(int)(CurrentSpectraItem - 1)].IntegrationTime.ToString() + "ms.spc";
                        double[] wl     = _phosSpectra[(int)(CurrentSpectraItem - 1)].Spectrum.Select(p => p.X).ToArray();
                        double[] counts = _phosSpectra[(int)(CurrentSpectraItem - 1)].Spectrum.Select(p => p.Y).ToArray();

                        if (!SPCHelper.SaveToSPC(wl, counts, spectraFileName, "Wavelength (nm)", "Intensity"))
                        {
                            throw new Exception("spectra save fail");
                        }
                    }

                    // save markers and image
                    var markerFileName = markerFolderPath + @"\pre_image" + "_" + CamResultsPre[0].TimeStamp.ToString() + "ms.jpg";
                    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(CamResultsPre[0].Image))
                    {
                        if (Path.GetExtension(markerFileName).ToUpper().Contains("JPG"))
                        {
                            bmp.Save(markerFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        else
                        {
                            bmp.Save(markerFileName);
                        }
                    }
                    var        markerListName = markerFolderPath + @"\markers" + ".txt";
                    TextWriter tw             = new StreamWriter(markerListName);
                    tw.WriteLine(MarkerList[(int)CurrentListItem].Item3.ToString());
                    tw.Close();
                }

                e.Result = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("phosresultvm exception: " + ex.Message);
                e.Result = false;
            }
            finally
            {
            }
        }