Exemplo n.º 1
0
        private void SaveToFile(Screenshot screenshot, Security security, int jpegQuality)
        {
            try
            {
                if (screenshot.Bitmap != null && screenshot.Format != null && !string.IsNullOrEmpty(screenshot.FilePath))
                {
                    if (screenshot.Format.Name.Equals("JPEG"))
                    {
                        var encoderParams = new EncoderParameters(1);
                        encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, jpegQuality);

                        var encoderInfo = GetEncoderInfo("image/jpeg");

                        screenshot.Bitmap.Save(screenshot.FilePath, encoderInfo, encoderParams);
                    }
                    else
                    {
                        screenshot.Bitmap.Save(screenshot.FilePath, screenshot.Format.Format);
                    }

                    if (screenshot.Encrypt)
                    {
                        string key = security.EncryptFile(screenshot.FilePath, screenshot.FilePath + "-encrypted");

                        if (!string.IsNullOrEmpty(key))
                        {
                            if (_fileSystem.FileExists(screenshot.FilePath))
                            {
                                if (_fileSystem.DeleteFile(screenshot.FilePath))
                                {
                                    _fileSystem.MoveFile(screenshot.FilePath + "-encrypted", screenshot.FilePath);

                                    screenshot.Key       = key;
                                    screenshot.Encrypt   = false;
                                    screenshot.Encrypted = true;
                                }
                            }
                        }
                    }

                    screenshot.Bitmap.Dispose();
                    screenshot.Bitmap = null;

                    screenshot.SavedToDisk = true;

                    _log.WriteMessage("Screenshot (id = " + screenshot.Id + ", viewid = " + screenshot.ViewId + ", encrypted = " + screenshot.Encrypted.ToString() + ") saved to \"" + screenshot.FilePath + "\"");
                }
            }
            catch
            {
                // We want to write to the error file instead of writing an exception just in case the user
                // has ExitOnError set and the exception causes the application to exit.
                _log.WriteErrorMessage("There was an error encountered when saving the screenshot image");
            }
        }