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);
        }
 public PrintHelper(Image image)
 {
     this.image = image;
     printDialog.UseEXDialog    = true;
     printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(AppConfig.GetInstance().Output_File_FilenamePattern);
     printDocument.PrintPage   += GetImageForPrint;
     printDialog.Document       = printDocument;
 }
Пример #3
0
 public PrintHelper(Image image, ICaptureDetails captureDetails)
 {
     this.image = image;
     printDialog.UseEXDialog    = true;
     printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(conf.OutputFileFilenamePattern, captureDetails);
     printDocument.PrintPage   += DrawImageForPrint;
     printDialog.Document       = printDocument;
 }
Пример #4
0
 public PrintHelper(ISurface surface, ICaptureDetails captureDetails)
 {
     _surface                    = surface;
     _captureDetails             = captureDetails;
     _printDialog.UseEXDialog    = true;
     _printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(CoreConfig.OutputFileFilenamePattern, captureDetails);
     _printDocument.PrintPage   += DrawImageForPrint;
     _printDialog.Document       = _printDocument;
 }
Пример #5
0
        public 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));
        }
Пример #6
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);
            }
        }
        /// <summary>
        /// Saves image to specific path with specified quality
        /// </summary>
        public static void Save(Image image, string fullPath, bool allowOverwrite, int quality, bool reduceColors, 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);
            }
            string extension = Path.GetExtension(fullPath);

            if (extension != null && extension.StartsWith("."))
            {
                extension = extension.Substring(1);
            }
            OutputFormat format = OutputFormat.png;

            try {
                if (extension != null)
                {
                    format = (OutputFormat)Enum.Parse(typeof(OutputFormat), extension.ToLower());
                }
            } catch (ArgumentException ae) {
                LOG.Warn("Couldn't parse extension: " + extension, ae);
            }
            if (!allowOverwrite && File.Exists(fullPath))
            {
                ArgumentException throwingException = new ArgumentException("File '" + fullPath + "' already exists.");
                throwingException.Data.Add("fullPath", fullPath);
                throw throwingException;
            }
            LOG.DebugFormat("Saving image to {0}", fullPath);
            // Create the stream and call SaveToStream
            using (FileStream stream = new FileStream(fullPath, FileMode.Create, FileAccess.Write)) {
                SaveToStream(image, stream, format, quality, reduceColors);
            }

            if (copyPathToClipboard)
            {
                ClipboardHelper.SetClipboardData(fullPath);
            }
        }
Пример #8
0
        public PrintHelper(
            ICoreConfiguration coreConfiguration,
            IGreenshotLanguage greenshotLanguage,
            Func <Owned <PrintOptionsDialog> > printOptionsDialogFactory,
            ISurface surface,
            ICaptureDetails captureDetails)
        {
            _coreConfig                = coreConfiguration;
            _greenshotLanguage         = greenshotLanguage;
            _printOptionsDialogFactory = printOptionsDialogFactory;

            _surface                    = surface;
            _captureDetails             = captureDetails;
            _printDialog.UseEXDialog    = true;
            _printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(coreConfiguration.OutputFileFilenamePattern, captureDetails);
            _printDocument.PrintPage   += DrawImageForPrint;
            _printDialog.Document       = _printDocument;
        }
Пример #9
0
        /// <summary>
        /// Helper Method for creating an Email with Attachment
        /// </summary>
        /// <param name="fullpath">Path to file</param>
        /// <param name="captureDetails"></param>
        public static void SendImage(string fullPath, ICaptureDetails captureDetails)
        {
            string          title   = FilenameHelper.FillPattern(conf.MailApiSubjectPattern, captureDetails, false);
            MapiMailMessage message = new MapiMailMessage(title, null);

            message.Files.Add(fullPath);
            if (!string.IsNullOrEmpty(conf.MailApiTo))
            {
                message._recipientCollection.Add(new Recipient(conf.MailApiTo, RecipientType.To));
            }
            if (!string.IsNullOrEmpty(conf.MailApiCC))
            {
                message._recipientCollection.Add(new Recipient(conf.MailApiCC, RecipientType.CC));
            }
            if (!string.IsNullOrEmpty(conf.MailApiBCC))
            {
                message._recipientCollection.Add(new Recipient(conf.MailApiBCC, RecipientType.BCC));
            }
            message.ShowDialog();
        }
Пример #10
0
        private void DrawImageForPrint(object sender, PrintPageEventArgs e)
        {
            // Create the output settins
            var printOutputSettings = new SurfaceOutputSettings(OutputFormats.png, 100, false);

            ApplyEffects(printOutputSettings);

            var disposeImage = ImageOutput.CreateBitmapFromSurface(_surface, printOutputSettings, out var bitmap);

            try
            {
                var alignment = _coreConfig.OutputPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft;

                // prepare timestamp
                float  footerStringWidth  = 0;
                float  footerStringHeight = 0;
                string footerString       = null;           //DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
                if (_coreConfig.OutputPrintFooter)
                {
                    footerString = FilenameHelper.FillPattern(_coreConfig.OutputPrintFooterPattern, _captureDetails, false);
                    using (var f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular))
                    {
                        footerStringWidth  = e.Graphics.MeasureString(footerString, f).Width;
                        footerStringHeight = e.Graphics.MeasureString(footerString, f).Height;
                    }
                }

                // Get a rectangle representing the printable Area
                var pageRect = e.PageSettings.PrintableArea;
                if (e.PageSettings.Landscape)
                {
                    var origWidth = pageRect.Width;
                    pageRect.Width  = pageRect.Height;
                    pageRect.Height = origWidth;
                }

                // Subtract the dateString height from the available area, this way the area stays free
                pageRect.Height -= footerStringHeight;

                var gu        = GraphicsUnit.Pixel;
                var imageRect = bitmap.GetBounds(ref gu);
                // rotate the image if it fits the page better
                if (_coreConfig.OutputPrintAllowRotate)
                {
                    if (pageRect.Width > pageRect.Height && imageRect.Width < imageRect.Height || pageRect.Width < pageRect.Height && imageRect.Width > imageRect.Height)
                    {
                        bitmap.RotateFlip(RotateFlipType.Rotate270FlipNone);
                        imageRect = bitmap.GetBounds(ref gu);
                        if (alignment.Equals(ContentAlignment.TopLeft))
                        {
                            alignment = ContentAlignment.TopRight;
                        }
                    }
                }
                NativeSizeFloat size      = imageRect.Size;
                var             printRect = new NativeRect(0, 0, size.Round());
                // scale the image to fit the page better
                if (_coreConfig.OutputPrintAllowEnlarge || _coreConfig.OutputPrintAllowShrink)
                {
                    var resizedRect = ScaleHelper.GetScaledSize(imageRect.Size, pageRect.Size, false);
                    if (_coreConfig.OutputPrintAllowShrink && resizedRect.Width < printRect.Width || _coreConfig.OutputPrintAllowEnlarge && resizedRect.Width > printRect.Width)
                    {
                        printRect = printRect.Resize(resizedRect.Round());
                    }
                }

                // align the image
                printRect = ScaleHelper.GetAlignedRectangle(printRect, new NativeRect(0, 0, new NativeSizeFloat(pageRect.Width, pageRect.Height).Round()), alignment).Round();
                if (_coreConfig.OutputPrintFooter)
                {
                    //printRect = new NativeRect(0, 0, printRect.Width, printRect.Height - (dateStringHeight * 2));
                    using (var f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular))
                    {
                        e.Graphics.DrawString(footerString, f, Brushes.Black, pageRect.Width / 2 - footerStringWidth / 2, pageRect.Height);
                    }
                }
                e.Graphics.DrawImage(bitmap, printRect, imageRect, GraphicsUnit.Pixel);
            }
            finally
            {
                if (disposeImage)
                {
                    bitmap?.Dispose();
                }
            }
        }
Пример #11
0
        void DrawImageForPrint(object sender, PrintPageEventArgs e)
        {
            // Create the output settins
            SurfaceOutputSettings printOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false);

            ApplyEffects(printOutputSettings);

            Image image;
            bool  disposeImage = ImageOutput.CreateImageFromSurface(surface, printOutputSettings, out image);

            try {
                ContentAlignment alignment = conf.OutputPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft;

                // prepare timestamp
                float  footerStringWidth  = 0;
                float  footerStringHeight = 0;
                string footerString       = null;           //DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
                if (conf.OutputPrintFooter)
                {
                    footerString = FilenameHelper.FillPattern(conf.OutputPrintFooterPattern, captureDetails, false);
                    using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
                        footerStringWidth  = e.Graphics.MeasureString(footerString, f).Width;
                        footerStringHeight = e.Graphics.MeasureString(footerString, f).Height;
                    }
                }

                // Get a rectangle representing the printable Area
                RectangleF pageRect = e.PageSettings.PrintableArea;
                if (e.PageSettings.Landscape)
                {
                    float origWidth = pageRect.Width;
                    pageRect.Width  = pageRect.Height;
                    pageRect.Height = origWidth;
                }

                // Subtract the dateString height from the available area, this way the area stays free
                pageRect.Height -= footerStringHeight;

                GraphicsUnit gu        = GraphicsUnit.Pixel;
                RectangleF   imageRect = image.GetBounds(ref gu);
                // rotate the image if it fits the page better
                if (conf.OutputPrintAllowRotate)
                {
                    if ((pageRect.Width > pageRect.Height && imageRect.Width < imageRect.Height) || (pageRect.Width < pageRect.Height && imageRect.Width > imageRect.Height))
                    {
                        image.RotateFlip(RotateFlipType.Rotate270FlipNone);
                        imageRect = image.GetBounds(ref gu);
                        if (alignment.Equals(ContentAlignment.TopLeft))
                        {
                            alignment = ContentAlignment.TopRight;
                        }
                    }
                }

                RectangleF printRect = new RectangleF(0, 0, imageRect.Width, imageRect.Height);
                // scale the image to fit the page better
                if (conf.OutputPrintAllowEnlarge || conf.OutputPrintAllowShrink)
                {
                    SizeF resizedRect = ScaleHelper.GetScaledSize(imageRect.Size, pageRect.Size, false);
                    if ((conf.OutputPrintAllowShrink && resizedRect.Width < printRect.Width) || conf.OutputPrintAllowEnlarge && resizedRect.Width > printRect.Width)
                    {
                        printRect.Size = resizedRect;
                    }
                }

                // align the image
                printRect = ScaleHelper.GetAlignedRectangle(printRect, new RectangleF(0, 0, pageRect.Width, pageRect.Height), alignment);
                if (conf.OutputPrintFooter)
                {
                    //printRect = new RectangleF(0, 0, printRect.Width, printRect.Height - (dateStringHeight * 2));
                    using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
                        e.Graphics.DrawString(footerString, f, Brushes.Black, pageRect.Width / 2 - (footerStringWidth / 2), pageRect.Height);
                    }
                }
                e.Graphics.DrawImage(image, printRect, imageRect, GraphicsUnit.Pixel);
            } finally {
                if (disposeImage && image != null)
                {
                    image.Dispose();
                    image = null;
                }
            }
        }
        void DrawImageForPrint(object sender, PrintPageEventArgs e)
        {
            ContentAlignment alignment = conf.OutputPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft;

            // prepare timestamp
            float  footerStringWidth  = 0;
            float  footerStringHeight = 0;
            string footerString       = null;       //DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();

            if (conf.OutputPrintFooter)
            {
                footerString = FilenameHelper.FillPattern(conf.OutputPrintFooterPattern, captureDetails, false);
                using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
                    footerStringWidth  = e.Graphics.MeasureString(footerString, f).Width;
                    footerStringHeight = e.Graphics.MeasureString(footerString, f).Height;
                }
            }

            // Get a rectangle representing the printable Area
            RectangleF pageRect = e.PageSettings.PrintableArea;

            // Subtract the dateString height from the available area, this way the area stays free
            pageRect.Height -= footerStringHeight;

            GraphicsUnit gu        = GraphicsUnit.Pixel;
            RectangleF   imageRect = image.GetBounds(ref gu);

            // rotate the image if it fits the page better
            if (conf.OutputPrintAllowRotate)
            {
                if ((pageRect.Width > pageRect.Height && imageRect.Width < imageRect.Height) || (pageRect.Width < pageRect.Height && imageRect.Width > imageRect.Height))
                {
                    image.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    imageRect = image.GetBounds(ref gu);
                    if (alignment.Equals(ContentAlignment.TopLeft))
                    {
                        alignment = ContentAlignment.TopRight;
                    }
                }
            }

            RectangleF printRect = new RectangleF(0, 0, imageRect.Width, imageRect.Height);

            // scale the image to fit the page better
            if (conf.OutputPrintAllowEnlarge || conf.OutputPrintAllowShrink)
            {
                SizeF resizedRect = ScaleHelper.GetScaledSize(imageRect.Size, pageRect.Size, false);
                if ((conf.OutputPrintAllowShrink && resizedRect.Width < printRect.Width) || conf.OutputPrintAllowEnlarge && resizedRect.Width > printRect.Width)
                {
                    printRect.Size = resizedRect;
                }
            }

            // align the image
            printRect = ScaleHelper.GetAlignedRectangle(printRect, new RectangleF(0, 0, pageRect.Width, pageRect.Height), alignment);
            if (conf.OutputPrintFooter)
            {
                //printRect = new RectangleF(0, 0, printRect.Width, printRect.Height - (dateStringHeight * 2));
                using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
                    e.Graphics.DrawString(footerString, f, Brushes.Black, pageRect.Width / 2 - (footerStringWidth / 2), pageRect.Height);
                }
            }
            if (conf.OutputPrintInverted)
            {
                using (Bitmap negativeBitmap = ImageHelper.CreateNegative((Bitmap)image)) {
                    e.Graphics.DrawImage(negativeBitmap, printRect, imageRect, GraphicsUnit.Pixel);
                }
            }
            else
            {
                e.Graphics.DrawImage(image, printRect, imageRect, GraphicsUnit.Pixel);
            }
        }