Example #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)
        {
            var pattern = CoreConfig.OutputFileFilenamePattern;

            if (string.IsNullOrEmpty(pattern?.Trim()))
            {
                pattern = "greenshot ${capturetime}";
            }
            var 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, @"_+", "_");
            var tmpFile = Path.Combine(Path.GetTempPath(), filename);

            Log.Debug().WriteLine("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
            {
                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 = SaveWithDialog(surface, captureDetails);
            }
            return(tmpFile);
        }
Example #2
0
        /// <summary>
        ///     Saves image to specific path with specified quality
        /// </summary>
        public static void Save(ISurface surface, string fullPath, bool allowOverwrite, SurfaceOutputSettings outputSettings, bool copyPathToClipboard)
        {
            fullPath = FilenameHelper.MakeFqFilenameSafe(fullPath);
            var path = Path.GetDirectoryName(fullPath);

            // check whether path exists - if not create it
            if (path != null)
            {
                var di = new DirectoryInfo(path);
                if (!di.Exists)
                {
                    Directory.CreateDirectory(di.FullName);
                }
            }

            if (!allowOverwrite && File.Exists(fullPath))
            {
                var throwingException = new ArgumentException("File '" + fullPath + "' already exists.");
                throwingException.Data.Add("fullPath", fullPath);
                throw throwingException;
            }
            Log.Debug().WriteLine("Saving surface to {0}", fullPath);
            // Create the stream and call SaveToStream
            using (var stream = new FileStream(fullPath, FileMode.Create, FileAccess.Write))
            {
                SaveToStream(surface, stream, outputSettings);
            }

            if (copyPathToClipboard)
            {
                ClipboardHelper.SetClipboardData(fullPath);
            }
        }
Example #3
0
        /// <summary>
        ///     Saves image to specific path with specified quality
        /// </summary>
        public static void Save(ISurface surface, string fullPath, bool allowOverwrite, SurfaceOutputSettings outputSettings, bool copyPathToClipboard)
        {
            fullPath = FilenameHelper.MakeFqFilenameSafe(fullPath);
            var path = Path.GetDirectoryName(fullPath);

            // check whether path exists - if not create it
            if (path != null)
            {
                var di = new DirectoryInfo(path);
                if (!di.Exists)
                {
                    Directory.CreateDirectory(di.FullName);
                }
            }

            if (!allowOverwrite && File.Exists(fullPath))
            {
                var throwingException = new ArgumentException("File '" + fullPath + "' already exists.");
                throwingException.Data.Add("fullPath", fullPath);
                throw throwingException;
            }
            Log.Debug().WriteLine("Saving surface to {0}", fullPath);
            // Create the stream and call SaveToStream
            using (var stream = new FileStream(fullPath, FileMode.Create, FileAccess.Write))
            {
                SaveToStream(surface, stream, outputSettings);
            }

            // TODO: This should not be done here, remove this!!
            if (copyPathToClipboard)
            {
                using (var clipboardAccessToken = ClipboardNative.Access())
                {
                    clipboardAccessToken.ClearContents();
                    // TODO: File??
                    clipboardAccessToken.SetAsUnicodeString(fullPath);
                }
            }
        }