Ejemplo n.º 1
0
        private void SaveScan()
        {
            SetInfoAboutPathToSaveImage();
            if (!PathToSaveImageNotExist && _webCamCapture.GetBitmap() is Bitmap image && image != null)
            {
                string targetFormat = Properties.Settings.Default.ImageFormat;
                string targetPath   = GetTargetPathToFolder();
                string scanName     = _deviceManager.SelectedDiviceName + "_scan_";
                int    scanIndex    = GetNextIndexOfScan(targetPath, scanName);

                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }

                image.Save(Path.Combine(targetPath, $"{scanName}{scanIndex}.{targetFormat}"), Imaging.GetAllowedImageFormatByName(targetFormat));
            }

            #region Helper methods

            string GetTargetPathToFolder()
            {
                bool   createFolderForCam = Properties.Settings.Default.CreateFolderForCam;
                string rootPath           = Properties.Settings.Default.PathToSaveImage;

                return(createFolderForCam ? Path.Combine(rootPath, _deviceManager.SelectedDiviceName) : rootPath);
            }

            int GetNextIndexOfScan(string targetPath, string scanName)
            {
                if (Directory.Exists(targetPath))
                {
                    var rgx         = new Regex($@"{scanName}(\d+)\.({string.Join("|", Imaging.AllowedImageFormatsNames)})$", RegexOptions.IgnoreCase);
                    int lastScanIdx = Directory.GetFiles(targetPath, scanName + "*").Select(x => Path.GetFileName(x))
                                      .Select(x => rgx.Match(x)).Where(m => m.Success).Select(m => int.Parse(m.Groups[1].Value)).DefaultIfEmpty().Max();
                    return(lastScanIdx + 1);
                }
                return(1);
            }

            #endregion
        }