private bool DownloadAndUpload() { string url = Info.Result.URL.Trim(); Info.Result.URL = ""; Info.FilePath = TaskHelpers.CheckFilePath(Info.TaskSettings.CaptureFolder, Info.FileName, Info.TaskSettings); if (!string.IsNullOrEmpty(Info.FilePath)) { Info.Status = Resources.UploadTask_DownloadAndUpload_Downloading; OnStatusChanged(); try { Helpers.CreateDirectoryFromFilePath(Info.FilePath); using (WebClient wc = new WebClient()) { wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy(); wc.DownloadFile(url, Info.FilePath); } LoadFileStream(); return(true); } catch (Exception e) { DebugHelper.WriteException(e); MessageBox.Show(string.Format(Resources.UploadManager_DownloadAndUploadFile_Download_failed, e), "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(false); }
public static void DownloadAndUploadFile(string url, TaskSettings taskSettings = null) { if (!string.IsNullOrEmpty(url)) { if (taskSettings == null) { taskSettings = TaskSettings.GetDefaultTaskSettings(); } string downloadPath = null; bool isDownloaded = false; TaskEx.Run(() => { url = url.Trim(); string filename = URLHelpers.GetFileName(url, true, true); if (!string.IsNullOrEmpty(filename)) { downloadPath = TaskHelpers.CheckFilePath(taskSettings.CaptureFolder, filename, taskSettings); if (!string.IsNullOrEmpty(downloadPath)) { Helpers.CreateDirectoryIfNotExist(downloadPath); try { using (WebClient wc = new WebClient()) { wc.Proxy = HelpersOptions.CurrentProxy.GetWebProxy(); wc.DownloadFile(url, downloadPath); } isDownloaded = true; } catch (Exception e) { DebugHelper.WriteException(e); MessageBox.Show(string.Format(Resources.UploadManager_DownloadAndUploadFile_Download_failed, e), "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }, () => { if (isDownloaded) { UploadFile(downloadPath, taskSettings); } }); } }
private void DoTextJobs() { if (Info.TaskSettings.AdvancedSettings.TextTaskSaveAsFile) { string filePath = TaskHelpers.CheckFilePath(Info.TaskSettings.CaptureFolder, Info.FileName, Info.TaskSettings); if (!string.IsNullOrEmpty(filePath)) { Info.FilePath = filePath; Helpers.CreateDirectoryFromFilePath(Info.FilePath); File.WriteAllText(Info.FilePath, tempText, Encoding.UTF8); DebugHelper.WriteLine("Text saved to file: " + Info.FilePath); } } byte[] byteArray = Encoding.UTF8.GetBytes(tempText); Data = new MemoryStream(byteArray); }
public static void DownloadAndUploadFile(string url, string filename, TaskSettings taskSettings = null) { if (!string.IsNullOrEmpty(url)) { if (taskSettings == null) { taskSettings = TaskSettings.GetDefaultTaskSettings(); } string downloadPath = null; bool isDownloaded = false; TaskEx.Run(() => { downloadPath = TaskHelpers.CheckFilePath(taskSettings.CaptureFolder, filename, taskSettings); if (!string.IsNullOrEmpty(downloadPath)) { try { using (WebClient wc = new WebClient()) { wc.Proxy = ProxyInfo.Current.GetWebProxy(); wc.DownloadFile(url, downloadPath); } isDownloaded = true; } catch (Exception e) { DebugHelper.WriteException(e); MessageBox.Show("Download failed:\r\n" + e.ToString(), "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }, () => { if (isDownloaded) { UploadFile(downloadPath, taskSettings); } }); } }
public static string CreateThumbnail(Image img, string folder, string filename, TaskSettings taskSettings) { if ((taskSettings.ImageSettings.ThumbnailWidth > 0 || taskSettings.ImageSettings.ThumbnailHeight > 0) && (!taskSettings.ImageSettings.ThumbnailCheckSize || (img.Width > taskSettings.ImageSettings.ThumbnailWidth && img.Height > taskSettings.ImageSettings.ThumbnailHeight))) { string thumbnailFileName = Path.GetFileNameWithoutExtension(filename) + taskSettings.ImageSettings.ThumbnailName + ".jpg"; string thumbnailFilePath = TaskHelpers.CheckFilePath(folder, thumbnailFileName, taskSettings); if (!string.IsNullOrEmpty(thumbnailFilePath)) { Image thumbImage = null; try { thumbImage = (Image)img.Clone(); thumbImage = new Resize { Width = taskSettings.ImageSettings.ThumbnailWidth, Height = taskSettings.ImageSettings.ThumbnailHeight }.Apply(thumbImage); thumbImage = ImageHelpers.FillBackground(thumbImage, Color.White); thumbImage.SaveJPG(thumbnailFilePath, 90); return(thumbnailFilePath); } finally { if (thumbImage != null) { thumbImage.Dispose(); } } } } return(null); }
private void DoAfterCaptureJobs() { if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.AddImageEffects)) { tempImage = TaskHelpers.AddImageEffects(tempImage, Info.TaskSettings); } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.AddWatermark) && Info.TaskSettings.ImageSettings.WatermarkConfig != null) { Info.TaskSettings.ImageSettings.WatermarkConfig.Apply(tempImage); } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.AnnotateImage)) { tempImage = TaskHelpers.AnnotateImage(tempImage); } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.CopyImageToClipboard)) { ClipboardHelpers.CopyImage(tempImage); DebugHelper.WriteLine("CopyImageToClipboard"); } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SendImageToPrinter)) { if (Program.Settings.DontShowPrintSettingsDialog) { using (PrintHelper printHelper = new PrintHelper(tempImage)) { printHelper.Settings = Program.Settings.PrintSettings; printHelper.Print(); } } else { using (PrintForm printForm = new PrintForm(tempImage, Program.Settings.PrintSettings)) { printForm.ShowDialog(); } } } if (Info.TaskSettings.AfterCaptureJob.HasFlagAny(AfterCaptureTasks.SaveImageToFile, AfterCaptureTasks.SaveImageToFileWithDialog, AfterCaptureTasks.UploadImageToHost)) { using (tempImage) { ImageData imageData = TaskHelpers.PrepareImage(tempImage, Info.TaskSettings); Data = imageData.ImageStream; Info.FileName = Path.ChangeExtension(Info.FileName, imageData.ImageFormat.GetDescription()); if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SaveImageToFile)) { string filePath = TaskHelpers.CheckFilePath(Info.TaskSettings.CaptureFolder, Info.FileName, Info.TaskSettings); if (!string.IsNullOrEmpty(filePath)) { Info.FilePath = filePath; imageData.Write(Info.FilePath); DebugHelper.WriteLine("SaveImageToFile: " + Info.FilePath); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SaveImageToFileWithDialog)) { using (SaveFileDialog sfd = new SaveFileDialog()) { if (string.IsNullOrEmpty(lastSaveAsFolder) || !Directory.Exists(lastSaveAsFolder)) { lastSaveAsFolder = Info.TaskSettings.CaptureFolder; } sfd.InitialDirectory = lastSaveAsFolder; sfd.FileName = Info.FileName; sfd.DefaultExt = Path.GetExtension(Info.FileName).Substring(1); sfd.Filter = string.Format("*{0}|*{0}|All files (*.*)|*.*", Path.GetExtension(Info.FileName)); sfd.Title = "Choose a folder to save " + Path.GetFileName(Info.FileName); if (sfd.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(sfd.FileName)) { Info.FilePath = sfd.FileName; lastSaveAsFolder = Path.GetDirectoryName(Info.FilePath); imageData.Write(Info.FilePath); DebugHelper.WriteLine("SaveImageToFileWithDialog: " + Info.FilePath); } } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SaveThumbnailImageToFile)) { string thumbnailFilename, thumbnailFolder; if (!string.IsNullOrEmpty(Info.FilePath)) { thumbnailFilename = Path.GetFileName(Info.FilePath); thumbnailFolder = Path.GetDirectoryName(Info.FilePath); } else { thumbnailFilename = Info.FileName; thumbnailFolder = Info.TaskSettings.CaptureFolder; } Info.ThumbnailFilePath = TaskHelpers.CreateThumbnail(tempImage, thumbnailFolder, thumbnailFilename, Info.TaskSettings); if (!string.IsNullOrEmpty(Info.ThumbnailFilePath)) { DebugHelper.WriteLine("SaveThumbnailImageToFile: " + Info.ThumbnailFilePath); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.CopyFileToClipboard) && !string.IsNullOrEmpty(Info.FilePath) && File.Exists(Info.FilePath)) { ClipboardHelpers.CopyFile(Info.FilePath); } else if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.CopyFilePathToClipboard) && !string.IsNullOrEmpty(Info.FilePath)) { ClipboardHelpers.CopyText(Info.FilePath); } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.PerformActions) && Info.TaskSettings.ExternalPrograms != null && !string.IsNullOrEmpty(Info.FilePath) && File.Exists(Info.FilePath)) { var actions = Info.TaskSettings.ExternalPrograms.Where(x => x.IsActive); if (actions.Count() > 0) { if (Data != null) { Data.Dispose(); } foreach (ExternalProgram fileAction in actions) { fileAction.Run(Info.FilePath); } Data = new FileStream(Info.FilePath, FileMode.Open, FileAccess.Read, FileShare.Read); } } } } }
private bool DoAfterCaptureJobs() { if (tempImage == null) { return(true); } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.AddImageEffects)) { tempImage = TaskHelpers.AddImageEffects(tempImage, Info.TaskSettings); if (tempImage == null) { DebugHelper.WriteLine("Error: Applying image effects resulted empty image."); return(false); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.AnnotateImage)) { tempImage = TaskHelpers.AnnotateImageForTask(tempImage, Info.FileName, Info.TaskSettings); if (tempImage == null) { return(false); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.CopyImageToClipboard)) { ClipboardHelpers.CopyImage(tempImage); DebugHelper.WriteLine("Image copied to clipboard."); } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SendImageToPrinter)) { TaskHelpers.PrintImage(tempImage); } if (Info.TaskSettings.AfterCaptureJob.HasFlagAny(AfterCaptureTasks.SaveImageToFile, AfterCaptureTasks.SaveImageToFileWithDialog, AfterCaptureTasks.DoOCR, AfterCaptureTasks.UploadImageToHost)) { using (tempImage) { ImageData imageData = TaskHelpers.PrepareImage(tempImage, Info.TaskSettings); Data = imageData.ImageStream; Info.FileName = Path.ChangeExtension(Info.FileName, imageData.ImageFormat.GetDescription()); if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SaveImageToFile)) { string filePath = TaskHelpers.CheckFilePath(Info.TaskSettings.CaptureFolder, Info.FileName, Info.TaskSettings); if (!string.IsNullOrEmpty(filePath)) { Info.FilePath = filePath; imageData.Write(Info.FilePath); DebugHelper.WriteLine("Image saved to file: " + Info.FilePath); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SaveImageToFileWithDialog)) { using (SaveFileDialog sfd = new SaveFileDialog()) { bool imageSaved; do { if (string.IsNullOrEmpty(lastSaveAsFolder) || !Directory.Exists(lastSaveAsFolder)) { lastSaveAsFolder = Info.TaskSettings.CaptureFolder; } sfd.InitialDirectory = lastSaveAsFolder; sfd.FileName = Info.FileName; sfd.DefaultExt = Path.GetExtension(Info.FileName).Substring(1); sfd.Filter = string.Format("*{0}|*{0}|All files (*.*)|*.*", Path.GetExtension(Info.FileName)); sfd.Title = Resources.UploadTask_DoAfterCaptureJobs_Choose_a_folder_to_save + " " + Path.GetFileName(Info.FileName); if (sfd.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(sfd.FileName)) { Info.FilePath = sfd.FileName; lastSaveAsFolder = Path.GetDirectoryName(Info.FilePath); imageSaved = imageData.Write(Info.FilePath); if (imageSaved) { DebugHelper.WriteLine("Image saved to file with dialog: " + Info.FilePath); } } else { break; } } while (!imageSaved); } } if (Info.TaskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.SaveThumbnailImageToFile)) { string thumbnailFilename, thumbnailFolder; if (!string.IsNullOrEmpty(Info.FilePath)) { thumbnailFilename = Path.GetFileName(Info.FilePath); thumbnailFolder = Path.GetDirectoryName(Info.FilePath); } else { thumbnailFilename = Info.FileName; thumbnailFolder = Info.TaskSettings.CaptureFolder; } Info.ThumbnailFilePath = TaskHelpers.CreateThumbnail(tempImage, thumbnailFolder, thumbnailFilename, Info.TaskSettings); if (!string.IsNullOrEmpty(Info.ThumbnailFilePath)) { DebugHelper.WriteLine("Thumbnail saved to file: " + Info.ThumbnailFilePath); } } } } return(true); }
private static void StartRecording(ScreenRecordOutput outputType, TaskSettings taskSettings, ScreenRecordStartMethod startMethod = ScreenRecordStartMethod.Region) { if (outputType == ScreenRecordOutput.FFmpeg && taskSettings.CaptureSettings.FFmpegOptions.VideoCodec == FFmpegVideoCodec.gif) { outputType = ScreenRecordOutput.GIF; } if (outputType == ScreenRecordOutput.FFmpeg) { DebugHelper.WriteLine("Starting screen recording. Video encoder: \"{0}\", Audio encoder: \"{1}\", FPS: {2}", taskSettings.CaptureSettings.FFmpegOptions.VideoCodec.GetDescription(), taskSettings.CaptureSettings.FFmpegOptions.AudioCodec.GetDescription(), taskSettings.CaptureSettings.ScreenRecordFPS); } else { DebugHelper.WriteLine("Starting screen recording. FPS: {0}", taskSettings.CaptureSettings.GIFFPS); } if (taskSettings.CaptureSettings.RunScreencastCLI) { if (!Program.Settings.VideoEncoders.IsValidIndex(taskSettings.CaptureSettings.VideoEncoderSelected)) { MessageBox.Show(Resources.ScreenRecordForm_StartRecording_There_is_no_valid_CLI_video_encoder_selected_, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } if (!Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected].IsValid()) { MessageBox.Show(Resources.ScreenRecordForm_StartRecording_CLI_video_encoder_file_does_not_exist__ + Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected].Path, "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } } if (outputType == ScreenRecordOutput.GIF) { taskSettings.CaptureSettings.FFmpegOptions.VideoCodec = FFmpegVideoCodec.gif; taskSettings.CaptureSettings.FFmpegOptions.UseCustomCommands = false; } if (!TaskHelpers.CheckFFmpeg(taskSettings)) { return; } if (!taskSettings.CaptureSettings.FFmpegOptions.IsSourceSelected) { MessageBox.Show(Resources.ScreenRecordForm_StartRecording_FFmpeg_video_and_audio_source_both_can_t_be__None__, "ShareX - " + Resources.ScreenRecordForm_StartRecording_FFmpeg_error, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } Rectangle captureRectangle = Rectangle.Empty; switch (startMethod) { case ScreenRecordStartMethod.Region: RegionCaptureTasks.GetRectangleRegion(out captureRectangle, taskSettings.CaptureSettings.SurfaceOptions); break; case ScreenRecordStartMethod.ActiveWindow: if (taskSettings.CaptureSettings.CaptureClientArea) { captureRectangle = CaptureHelpers.GetActiveWindowClientRectangle(); } else { captureRectangle = CaptureHelpers.GetActiveWindowRectangle(); } break; case ScreenRecordStartMethod.CustomRegion: captureRectangle = taskSettings.CaptureSettings.CaptureCustomRegion; break; case ScreenRecordStartMethod.LastRegion: captureRectangle = Program.Settings.ScreenRecordRegion; break; } Rectangle screenRectangle = CaptureHelpers.GetScreenBounds(); captureRectangle = Rectangle.Intersect(captureRectangle, screenRectangle); if (outputType != ScreenRecordOutput.GIF) { captureRectangle = CaptureHelpers.EvenRectangleSize(captureRectangle); } if (IsRecording || !captureRectangle.IsValid() || screenRecorder != null) { return; } Program.Settings.ScreenRecordRegion = captureRectangle; IsRecording = true; string path = ""; bool abortRequested = false; float duration = taskSettings.CaptureSettings.ScreenRecordFixedDuration ? taskSettings.CaptureSettings.ScreenRecordDuration : 0; recordForm = new ScreenRecordForm(captureRectangle, startMethod == ScreenRecordStartMethod.Region, duration); recordForm.StopRequested += StopRecording; recordForm.Show(); TaskEx.Run(() => { try { string filename = TaskHelpers.GetFilename(taskSettings, taskSettings.CaptureSettings.FFmpegOptions.Extension); path = TaskHelpers.CheckFilePath(taskSettings.CaptureFolder, filename, taskSettings); if (string.IsNullOrEmpty(path)) { abortRequested = true; } if (!abortRequested) { recordForm.ChangeState(ScreenRecordState.BeforeStart); if (taskSettings.CaptureSettings.ScreenRecordAutoStart) { int delay = (int)(taskSettings.CaptureSettings.ScreenRecordStartDelay * 1000); if (delay > 0) { recordForm.InvokeSafe(() => recordForm.StartCountdown(delay)); recordForm.RecordResetEvent.WaitOne(delay); } } else { recordForm.RecordResetEvent.WaitOne(); } if (recordForm.IsAbortRequested) { abortRequested = true; } if (!abortRequested) { ScreencastOptions options = new ScreencastOptions() { FFmpeg = taskSettings.CaptureSettings.FFmpegOptions, ScreenRecordFPS = taskSettings.CaptureSettings.ScreenRecordFPS, GIFFPS = taskSettings.CaptureSettings.GIFFPS, Duration = duration, OutputPath = path, CaptureArea = captureRectangle, DrawCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor }; Screenshot screenshot = TaskHelpers.GetScreenshot(taskSettings); screenshot.CaptureCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor; screenRecorder = new ScreenRecorder(ScreenRecordOutput.FFmpeg, options, screenshot, captureRectangle); screenRecorder.RecordingStarted += () => recordForm.ChangeState(ScreenRecordState.AfterRecordingStart); recordForm.ChangeState(ScreenRecordState.AfterStart); screenRecorder.StartRecording(); if (recordForm.IsAbortRequested) { abortRequested = true; } } } } catch (Exception e) { DebugHelper.WriteException(e); } try { if (!abortRequested && screenRecorder != null && File.Exists(path)) { recordForm.ChangeState(ScreenRecordState.AfterStop); string sourceFilePath = path; if (outputType == ScreenRecordOutput.GIF) { path = Path.Combine(taskSettings.CaptureFolder, TaskHelpers.GetFilename(taskSettings, "gif")); screenRecorder.FFmpegEncodeAsGIF(sourceFilePath, path, Program.ToolsFolder); } if (taskSettings.CaptureSettings.RunScreencastCLI) { VideoEncoder encoder = Program.Settings.VideoEncoders[taskSettings.CaptureSettings.VideoEncoderSelected]; path = Path.Combine(taskSettings.CaptureFolder, TaskHelpers.GetFilename(taskSettings, encoder.OutputExtension)); screenRecorder.EncodeUsingCommandLine(encoder, sourceFilePath, path); } } } finally { if (recordForm != null) { recordForm.InvokeSafe(() => { recordForm.Close(); recordForm.Dispose(); recordForm = null; }); } if (screenRecorder != null) { if ((outputType == ScreenRecordOutput.GIF || taskSettings.CaptureSettings.RunScreencastCLI) && !string.IsNullOrEmpty(screenRecorder.CachePath) && File.Exists(screenRecorder.CachePath)) { File.Delete(screenRecorder.CachePath); } screenRecorder.Dispose(); screenRecorder = null; if (abortRequested && !string.IsNullOrEmpty(path) && File.Exists(path)) { File.Delete(path); } } } }, () => { string customFileName; if (!abortRequested && !string.IsNullOrEmpty(path) && File.Exists(path) && TaskHelpers.ShowAfterCaptureForm(taskSettings, out customFileName, null, path)) { if (!string.IsNullOrEmpty(customFileName)) { string currentFilename = Path.GetFileNameWithoutExtension(path); string ext = Path.GetExtension(path); if (!currentFilename.Equals(customFileName, StringComparison.InvariantCultureIgnoreCase)) { path = Helpers.RenameFile(path, customFileName + ext); } } WorkerTask task = WorkerTask.CreateFileJobTask(path, taskSettings, customFileName); TaskManager.Start(task); } abortRequested = false; IsRecording = false; }); }