예제 #1
0
        /// <summary>
        /// Create a tmpfile which has the name like in the configured pattern.
        /// Used e.g. by the email export
        /// </summary>
        /// <param name="surface"></param>
        /// <param name="captureDetails"></param>
        /// <param name="outputSettings"></param>
        /// <returns>Path to image file</returns>
        public static string SaveNamedTmpFile(ISurface surface, ICaptureDetails captureDetails, SurfaceOutputSettings outputSettings)
        {
            string pattern = conf.OutputFileFilenamePattern;

            if (pattern == null || string.IsNullOrEmpty(pattern.Trim()))
            {
                pattern = "greenshot ${capturetime}";
            }
            string filename = FilenameHelper.GetFilenameFromPattern(pattern, outputSettings.Format, captureDetails);

            // Prevent problems with "other characters", which causes a problem in e.g. Outlook 2007 or break our HTML
            filename = Regex.Replace(filename, @"[^\d\w\.]", "_");
            // Remove multiple "_"
            filename = Regex.Replace(filename, @"_+", "_");
            string tmpFile = Path.Combine(Path.GetTempPath(), filename);

            LOG.Debug("Creating TMP File: " + tmpFile);

            // Catching any exception to prevent that the user can't write in the directory.
            // This is done for e.g. bugs #2974608, #2963943, #2816163, #2795317, #2789218
            try {
                ImageOutput.Save(surface, tmpFile, true, outputSettings, false);
                tmpFileCache.Add(tmpFile, tmpFile);
            } catch (Exception e) {
                // Show the problem
                MessageBox.Show(e.Message, "Error");
                // when save failed we present a SaveWithDialog
                tmpFile = ImageOutput.SaveWithDialog(surface, captureDetails);
            }
            return(tmpFile);
        }
예제 #2
0
        /// <summary>
        /// Save with showing a dialog
        /// </summary>
        /// <param name="surface"></param>
        /// <param name="captureDetails"></param>
        /// <returns>Path to filename</returns>
        public static string SaveWithDialog(ISurface surface, ICaptureDetails captureDetails)
        {
            string returnValue = null;

            using (SaveImageFileDialog saveImageFileDialog = new SaveImageFileDialog(captureDetails)) {
                DialogResult dialogResult = saveImageFileDialog.ShowDialog();
                if (dialogResult.Equals(DialogResult.OK))
                {
                    try {
                        string fileNameWithExtension         = saveImageFileDialog.FileNameWithExtension;
                        SurfaceOutputSettings outputSettings = new SurfaceOutputSettings(FormatForFilename(fileNameWithExtension));
                        if (conf.OutputFilePromptQuality)
                        {
                            QualityDialog qualityDialog = new QualityDialog(outputSettings);
                            qualityDialog.ShowDialog();
                        }
                        // TODO: For now we always overwrite, should be changed
                        ImageOutput.Save(surface, fileNameWithExtension, true, outputSettings, conf.OutputFileCopyPathToClipboard);
                        returnValue = fileNameWithExtension;
                        IniConfig.Save();
                    } catch (System.Runtime.InteropServices.ExternalException) {
                        MessageBox.Show(Language.GetFormattedString("error_nowriteaccess", saveImageFileDialog.FileName).Replace(@"\\", @"\"), Language.GetString("error"));
                    }
                }
            }
            return(returnValue);
        }
예제 #3
0
        /// <summary>
        /// Helper method to create a temp image file
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static string SaveToTmpFile(ISurface surface, SurfaceOutputSettings outputSettings, string destinationPath)
        {
            string tmpFile = Path.GetRandomFileName() + "." + outputSettings.Format.ToString();

            // Prevent problems with "other characters", which could cause problems
            tmpFile = Regex.Replace(tmpFile, @"[^\d\w\.]", "");
            if (destinationPath == null)
            {
                destinationPath = Path.GetTempPath();
            }
            string tmpPath = Path.Combine(destinationPath, tmpFile);

            LOG.Debug("Creating TMP File : " + tmpPath);

            try {
                ImageOutput.Save(surface, tmpPath, true, outputSettings, false);
                tmpFileCache.Add(tmpPath, tmpPath);
            } catch (Exception) {
                return(null);
            }
            return(tmpPath);
        }