/// <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); string path = Path.GetDirectoryName(fullPath); // check whether path exists - if not create it DirectoryInfo di = new DirectoryInfo(path); if (!di.Exists) { Directory.CreateDirectory(di.FullName); } if (!allowOverwrite && File.Exists(fullPath)) { ArgumentException throwingException = new ArgumentException("File '" + fullPath + "' already exists."); throwingException.Data.Add("fullPath", fullPath); throw throwingException; } LOG.DebugFormat("Saving surface to {0}", fullPath); // Create the stream and call SaveToStream using (FileStream stream = new FileStream(fullPath, FileMode.Create, FileAccess.Write)) { SaveToStream(surface, stream, outputSettings); } if (copyPathToClipboard) { ClipboardHelper.SetClipboardData(fullPath); } }
/// <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 { 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); }
/// <summary> /// Return a filename for the current image format (png,jpg etc) with the default file pattern /// that is specified in the configuration /// </summary> /// <param name="format">A string with the format</param> /// <returns>The filename which should be used to save the image</returns> public static string GetFilename(OutputFormat format, ICaptureDetails captureDetails) { string pattern = conf.OutputFileFilenamePattern; if (pattern == null || string.IsNullOrEmpty(pattern.Trim())) { pattern = "greenshot ${capturetime}"; } return(FilenameHelper.GetFilenameFromPattern(pattern, format, captureDetails)); }