public static string SaveNamedTmpFile(Image image, ICaptureDetails captureDetails, OutputFormat outputFormat, int quality, bool reduceColors)
        {
            string pattern = conf.OutputFileFilenamePattern;

            if (pattern == null || string.IsNullOrEmpty(pattern.Trim()))
            {
                pattern = "greenshot ${capturetime}";
            }
            string filename = FilenameHelper.GetFilenameFromPattern(pattern, outputFormat, 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(image, tmpFile, true, quality, reduceColors, 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(image, captureDetails);
            }
            return(tmpFile);
        }
Esempio n. 2
0
        //private static string eagerlyCreatedDir = null;
        public static string SaveWithDialog(Image img)
        {
            string         ret  = null;
            AppConfig      conf = AppConfig.GetInstance();
            SaveFileDialog sfd  = CreateSaveFileDialog();
            DialogResult   dr   = sfd.ShowDialog();

            if (dr.Equals(DialogResult.OK))
            {
                try
                {
                    string fn = GetFileNameWithExtension(sfd);
                    ImageOutput.Save(img, fn);
                    ret = fn;
                    conf.Output_FileAs_Fullpath = fn;
                    conf.Store();
                }
                catch (System.Runtime.InteropServices.ExternalException)
                {
                    MessageBox.Show(Language.GetInstance().GetString("error_nowriteaccess").Replace("%path%", sfd.FileName).Replace(@"\\", @"\"), Language.GetInstance().GetString("error"));
                    //eagerlyCreatedDir = null;
                }
            }
            // clean up dir we have created when creating SaveFileDialog (if any)

            /*if(eagerlyCreatedDir != null) {
             *      DirectoryInfo di = new DirectoryInfo(eagerlyCreatedDir);
             *      if(di.Exists && di.GetFiles().Length == 0) {
             *              di.Delete();
             *      }
             *      eagerlyCreatedDir = null;
             * }*/
            return(ret);
        }
Esempio n. 3
0
        /// <summary>
        /// Helper Method for creating an Email with Image Attachment
        /// </summary>
        /// <param name="image">The image to send</param>
        /// <param name="captureDetails">ICaptureDetails</param>
        public static void SendImage(Image image, ICaptureDetails captureDetails)
        {
            string pattern = conf.OutputFileFilenamePattern;

            if (pattern == null || string.IsNullOrEmpty(pattern.Trim()))
            {
                pattern = "greenshot ${capturetime}";
            }
            string filename = FilenameHelper.GetFilenameFromPattern(pattern, conf.OutputFileFormat, 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 for Email: " + 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(image, tmpFile, true, conf.OutputFileJpegQuality, false);
            } catch (Exception e) {
                // Show the problem
                MessageBox.Show(e.Message, "Error");
                // when save failed we present a SaveWithDialog
                tmpFile = ImageOutput.SaveWithDialog(image, captureDetails);
            }

            if (tmpFile != null)
            {
                // Store the list of currently active windows, so we can make sure we show the email window later!
                List <WindowDetails> windowsBefore = WindowDetails.GetVisibleWindows();
                bool isEmailSend = false;
                if (EmailConfigHelper.HasOutlook() && (conf.OutputEMailFormat == EmailFormat.OUTLOOK_HTML || conf.OutputEMailFormat == EmailFormat.OUTLOOK_TXT))
                {
                    isEmailSend = OutlookExporter.ExportToOutlook(tmpFile, captureDetails);
                }
                if (!isEmailSend && EmailConfigHelper.HasMAPI())
                {
                    // Fallback to MAPI
                    // Send the email and Cleanup the tmp files on exit
                    SendImage(tmpFile, captureDetails.Title, true);
                }
                WindowDetails.ActiveNewerWindows(windowsBefore);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Helper method to create a temp image file
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static string SaveToTmpFile(Image image)
        {
            string tmpFile = Path.GetRandomFileName() + "." + conf.OutputFileFormat.ToString();

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

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

            try {
                ImageOutput.Save(image, tmpPath, true, conf.OutputFileJpegQuality, false);
            } catch (Exception) {
                return(null);
            }
            return(tmpPath);
        }
        public static string SaveWithDialog(Image image, ICaptureDetails captureDetails)
        {
            string returnValue = null;
            SaveImageFileDialog saveImageFileDialog = new SaveImageFileDialog(captureDetails);
            DialogResult        dialogResult        = saveImageFileDialog.ShowDialog();

            if (dialogResult.Equals(DialogResult.OK))
            {
                try {
                    string fileNameWithExtension = saveImageFileDialog.FileNameWithExtension;
                    // TODO: For now we overwrite, should be changed
                    ImageOutput.Save(image, fileNameWithExtension, true);
                    returnValue = fileNameWithExtension;
                    conf.OutputFileAsFullpath = fileNameWithExtension;
                    IniConfig.Save();
                } catch (System.Runtime.InteropServices.ExternalException) {
                    MessageBox.Show(Language.GetFormattedString(LangKey.error_nowriteaccess, saveImageFileDialog.FileName).Replace(@"\\", @"\"), Language.GetString(LangKey.error));
                }
            }
            return(returnValue);
        }
        //private static string eagerlyCreatedDir = null;

        public static string SaveWithDialog(Image img)
        {
            string         ret  = null;
            AppConfig      conf = AppConfig.GetInstance();
            SaveFileDialog sfd  = CreateSaveFileDialog();

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string fn = GetFileNameWithExtension(sfd);
                    ImageOutput.Save(img, fn);
                    ret = fn;
                    conf.Output_FileAs_Fullpath = fn;
                    conf.Save();
                }
                catch (System.Runtime.InteropServices.ExternalException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            return(ret);
        }