protected override TaskMetadata Execute(TaskSettings taskSettings) { WindowInfo windowInfo = new WindowInfo(WindowHandle); if (windowInfo.IsMinimized) { windowInfo.Restore(); } windowInfo.Activate(); Thread.Sleep(250); TaskMetadata metadata = new TaskMetadata(); metadata.UpdateInfo(windowInfo); if (taskSettings.CaptureSettings.CaptureTransparent && !taskSettings.CaptureSettings.CaptureClientArea) { metadata.Image = TaskHelpers.GetScreenshot(taskSettings).CaptureWindowTransparent(WindowHandle); } else { metadata.Image = TaskHelpers.GetScreenshot(taskSettings).CaptureWindow(WindowHandle); } return(metadata); }
private void CaptureInternal(TaskSettings taskSettings, bool autoHideForm) { if (autoHideForm && AllowAutoHideForm) { Program.MainForm.Hide(); Thread.Sleep(250); } TaskMetadata metadata = null; try { AllowAnnotation = true; metadata = Execute(taskSettings); } catch (Exception ex) { DebugHelper.WriteException(ex); } finally { if (autoHideForm && AllowAutoHideForm) { Program.MainForm.ForceActivate(); } AfterCapture(metadata, taskSettings); } }
private static string ProcessTwoPassEncoding(string input, TaskMetadata metadata, TaskSettings taskSettings, bool deleteInputFile = true) { string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(taskSettings, metadata); string fileName = TaskHelpers.GetFileName(taskSettings, taskSettings.CaptureSettings.FFmpegOptions.Extension, metadata); string output = Path.Combine(screenshotsFolder, fileName); try { if (taskSettings.CaptureSettings.FFmpegOptions.VideoCodec == FFmpegVideoCodec.gif) { screenRecorder.FFmpegEncodeAsGIF(input, output); } else { screenRecorder.FFmpegEncodeVideo(input, output); } } finally { if (deleteInputFile && !input.Equals(output, StringComparison.InvariantCultureIgnoreCase) && File.Exists(input)) { File.Delete(input); } } return(output); }
public static WorkerTask CreateFileJobTask(string filePath, TaskMetadata metadata, TaskSettings taskSettings, string customFileName = null) { WorkerTask task = new WorkerTask(taskSettings); task.Info.FilePath = filePath; task.Info.DataType = TaskHelpers.FindDataType(task.Info.FilePath, taskSettings); if (!string.IsNullOrEmpty(customFileName)) { string ext = Helpers.GetFileNameExtension(task.Info.FilePath); task.Info.FileName = Helpers.AppendExtension(customFileName, ext); } else if (task.Info.TaskSettings.UploadSettings.FileUploadUseNamePattern) { string ext = Helpers.GetFileNameExtension(task.Info.FilePath); task.Info.FileName = TaskHelpers.GetFileName(task.Info.TaskSettings, ext); } task.Info.Metadata = metadata; task.Info.Job = TaskJob.Job; if (task.Info.IsUploadJob && !task.LoadFileStream()) { return(null); } return(task); }
protected override TaskMetadata Execute(TaskSettings taskSettings) { TaskMetadata metadata = CreateMetadata(MonitorRectangle); metadata.Image = TaskHelpers.GetScreenshot().CaptureRectangle(MonitorRectangle); return(metadata); }
protected override TaskMetadata Execute(TaskSettings taskSettings) { Rectangle rect = taskSettings.CaptureSettings.CaptureCustomRegion; TaskMetadata metadata = CreateMetadata(rect); metadata.Image = TaskHelpers.GetScreenshot(taskSettings).CaptureRectangle(rect); return(metadata); }
protected override TaskMetadata Execute(TaskSettings taskSettings) { Rectangle rect = CaptureHelpers.GetScreenWorkingArea(); TaskMetadata metadata = CreateMetadata(rect); metadata.Image = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen(); return(metadata); }
protected TaskMetadata ExecuteRegionCapture(TaskSettings taskSettings) { TaskMetadata metadata = new TaskMetadata(); RegionCaptureMode mode; if (taskSettings.AdvancedSettings.RegionCaptureDisableAnnotation) { mode = RegionCaptureMode.Default; } else { mode = RegionCaptureMode.Annotation; } Screenshot screenshot = TaskHelpers.GetScreenshot(taskSettings); screenshot.CaptureCursor = false; Bitmap bmp = screenshot.CaptureFullscreen(); CursorData cursorData = null; if (taskSettings.CaptureSettings.ShowCursor) { cursorData = new CursorData(); } using (RegionCaptureForm form = new RegionCaptureForm(mode, taskSettings.CaptureSettingsReference.SurfaceOptions, bmp)) { if (cursorData != null && cursorData.IsVisible) { form.AddCursor(cursorData.Handle, CaptureHelpers.ScreenToClient(cursorData.Position)); } form.ShowDialog(); metadata.Image = form.GetResultImage(); if (metadata.Image != null) { if (form.IsImageModified) { AllowAnnotation = false; } if (form.Result == RegionResult.Region) { WindowInfo windowInfo = form.GetWindowInfo(); metadata.UpdateInfo(windowInfo); } lastRegionCaptureType = RegionCaptureType.Default; } } return(metadata); }
public AfterCaptureForm(TaskMetadata metadata, TaskSettings taskSettings) : this(taskSettings) { if (metadata != null && metadata.Image != null) { pbImage.LoadImage(metadata.Image); btnCopy.Enabled = true; } FileName = TaskHelpers.GetFileName(TaskSettings, null, metadata); txtFileName.Text = FileName; }
public TaskInfo(TaskSettings taskSettings) { if (taskSettings == null) { taskSettings = TaskSettings.GetDefaultTaskSettings(); } TaskSettings = taskSettings; Metadata = new TaskMetadata(); Result = new UploadResult(); }
protected TaskMetadata CreateMetadata(Rectangle insideRect, string ignoreProcess) { TaskMetadata metadata = new TaskMetadata(); IntPtr handle = NativeMethods.GetForegroundWindow(); WindowInfo windowInfo = new WindowInfo(handle); if ((ignoreProcess == null || !windowInfo.ProcessName.Equals(ignoreProcess, StringComparison.InvariantCultureIgnoreCase)) && (insideRect.IsEmpty || windowInfo.Rectangle.Contains(insideRect))) { metadata.UpdateInfo(windowInfo); } return(metadata); }
public static void RunImageTask(TaskMetadata metadata, TaskSettings taskSettings, bool skipQuickTaskMenu = false, bool skipAfterCaptureWindow = false) { if (taskSettings == null) { taskSettings = TaskSettings.GetDefaultTaskSettings(); } if (metadata != null && metadata.Image != null && taskSettings != null) { if (!skipQuickTaskMenu && taskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.ShowQuickTaskMenu)) { QuickTaskMenu quickTaskMenu = new QuickTaskMenu(); quickTaskMenu.TaskInfoSelected += taskInfo => { if (taskInfo == null) { RunImageTask(metadata, taskSettings, true); } else if (taskInfo.IsValid) { taskSettings.AfterCaptureJob = taskInfo.AfterCaptureTasks; taskSettings.AfterUploadJob = taskInfo.AfterUploadTasks; RunImageTask(metadata, taskSettings, true); } }; quickTaskMenu.ShowMenu(); return; } string customFileName = null; if (!skipAfterCaptureWindow && !TaskHelpers.ShowAfterCaptureForm(taskSettings, out customFileName, metadata)) { return; } WorkerTask task = WorkerTask.CreateImageUploaderTask(metadata, taskSettings, customFileName); TaskManager.Start(task); } }
public static WorkerTask CreateImageUploaderTask(TaskMetadata metadata, TaskSettings taskSettings, string customFileName = null) { WorkerTask task = new WorkerTask(taskSettings); task.Info.Job = TaskJob.Job; task.Info.DataType = EDataType.Image; if (!string.IsNullOrEmpty(customFileName)) { task.Info.FileName = Helpers.AppendExtension(customFileName, "bmp"); } else { task.Info.FileName = TaskHelpers.GetFileName(taskSettings, "bmp", metadata); } task.Info.Metadata = metadata; task.Image = metadata.Image; return(task); }
private void AfterCapture(TaskMetadata metadata, TaskSettings taskSettings) { if (metadata != null && metadata.Image != null) { if (taskSettings.GeneralSettings.PlaySoundAfterCapture) { TaskHelpers.PlayCaptureSound(taskSettings); } if (taskSettings.AfterCaptureJob.HasFlag(AfterCaptureTasks.AnnotateImage) && !AllowAnnotation) { taskSettings.AfterCaptureJob = taskSettings.AfterCaptureJob.Remove(AfterCaptureTasks.AnnotateImage); } if (taskSettings.ImageSettings.ImageEffectOnlyRegionCapture && GetType() != typeof(CaptureRegion) && GetType() != typeof(CaptureLastRegion)) { taskSettings.AfterCaptureJob = taskSettings.AfterCaptureJob.Remove(AfterCaptureTasks.AddImageEffects); } UploadManager.RunImageTask(metadata, taskSettings); } }
private static void StartRecording(ScreenRecordOutput outputType, TaskSettings taskSettings, ScreenRecordStartMethod startMethod = ScreenRecordStartMethod.Region) { if (outputType == ScreenRecordOutput.GIF) { taskSettings.CaptureSettings.FFmpegOptions.VideoCodec = FFmpegVideoCodec.gif; } if (taskSettings.CaptureSettings.FFmpegOptions.IsAnimatedImage) { taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding = true; } int fps; if (taskSettings.CaptureSettings.FFmpegOptions.VideoCodec == FFmpegVideoCodec.gif) { fps = taskSettings.CaptureSettings.GIFFPS; } else { fps = taskSettings.CaptureSettings.ScreenRecordFPS; } DebugHelper.WriteLine("Starting screen recording. Video encoder: \"{0}\", Audio encoder: \"{1}\", FPS: {2}", taskSettings.CaptureSettings.FFmpegOptions.VideoCodec.GetDescription(), taskSettings.CaptureSettings.FFmpegOptions.AudioCodec.GetDescription(), fps); if (!TaskHelpers.CheckFFmpeg(taskSettings)) { return; } if (!taskSettings.CaptureSettings.FFmpegOptions.IsSourceSelected) { MessageBox.Show(Resources.FFmpeg_FFmpeg_video_and_audio_source_both_can_t_be__None__, "ShareX - " + Resources.FFmpeg_FFmpeg_error, MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } Rectangle captureRectangle = Rectangle.Empty; TaskMetadata metadata = new TaskMetadata(); switch (startMethod) { case ScreenRecordStartMethod.Region: if (taskSettings.CaptureSettings.ScreenRecordTransparentRegion) { RegionCaptureTasks.GetRectangleRegionTransparent(out captureRectangle); } else { RegionCaptureTasks.GetRectangleRegion(out captureRectangle, out WindowInfo windowInfo, taskSettings.CaptureSettings.SurfaceOptions); metadata.UpdateInfo(windowInfo); } 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 (taskSettings.CaptureSettings.FFmpegOptions.IsEvenSizeRequired) { 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) { ActivateWindow = startMethod == ScreenRecordStartMethod.Region, Duration = duration, AskConfirmationOnAbort = taskSettings.CaptureSettings.ScreenRecordAskConfirmationOnAbort }; recordForm.StopRequested += StopRecording; recordForm.Show(); Task.Run(() => { try { string extension; if (taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding) { extension = "mp4"; } else { extension = taskSettings.CaptureSettings.FFmpegOptions.Extension; } string screenshotsFolder = TaskHelpers.GetScreenshotsFolder(taskSettings, metadata); string fileName = TaskHelpers.GetFileName(taskSettings, extension, metadata); path = TaskHelpers.HandleExistsFile(screenshotsFolder, 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() { IsRecording = true, IsLossless = taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding, FFmpeg = taskSettings.CaptureSettings.FFmpegOptions, FPS = fps, 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 += ScreenRecorder_RecordingStarted; screenRecorder.EncodingProgressChanged += ScreenRecorder_EncodingProgressChanged; recordForm.ChangeState(ScreenRecordState.AfterStart); screenRecorder.StartRecording(); if (recordForm.IsAbortRequested) { abortRequested = true; } } } } catch (Exception e) { DebugHelper.WriteException(e); } if (taskSettings.CaptureSettings.ScreenRecordTwoPassEncoding && !abortRequested && screenRecorder != null && File.Exists(path)) { recordForm.ChangeState(ScreenRecordState.Encoding); path = ProcessTwoPassEncoding(path, metadata, taskSettings); } if (recordForm != null) { recordForm.InvokeSafe(() => { recordForm.Close(); recordForm.Dispose(); recordForm = null; }); } if (screenRecorder != null) { screenRecorder.Dispose(); screenRecorder = null; if (abortRequested && !string.IsNullOrEmpty(path) && File.Exists(path)) { File.Delete(path); } } }).ContinueInCurrentContext(() => { if (!abortRequested && !string.IsNullOrEmpty(path) && File.Exists(path) && TaskHelpers.ShowAfterCaptureForm(taskSettings, out string 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, metadata, taskSettings, customFileName); TaskManager.Start(task); } abortRequested = false; IsRecording = false; }); }
public static void RunImageTask(Bitmap bmp, TaskSettings taskSettings, bool skipQuickTaskMenu = false, bool skipAfterCaptureWindow = false) { TaskMetadata metadata = new TaskMetadata(bmp); RunImageTask(metadata, taskSettings, skipQuickTaskMenu, skipAfterCaptureWindow); }
protected TaskMetadata ExecuteRegionCapture(TaskSettings taskSettings) { RegionCaptureMode mode; if (taskSettings.AdvancedSettings.RegionCaptureDisableAnnotation) { mode = RegionCaptureMode.Default; } else { mode = RegionCaptureMode.Annotation; } Bitmap canvas; Screenshot screenshot = TaskHelpers.GetScreenshot(taskSettings); screenshot.CaptureCursor = false; if (taskSettings.CaptureSettings.SurfaceOptions.ActiveMonitorMode) { canvas = screenshot.CaptureActiveMonitor(); } else { canvas = screenshot.CaptureFullscreen(); } CursorData cursorData = null; if (taskSettings.CaptureSettings.ShowCursor) { cursorData = new CursorData(); } using (RegionCaptureForm form = new RegionCaptureForm(mode, taskSettings.CaptureSettingsReference.SurfaceOptions, canvas)) { if (cursorData != null && cursorData.IsVisible) { form.AddCursor(cursorData.ToBitmap(), form.PointToClient(cursorData.DrawPosition)); } form.ShowDialog(); Bitmap result = form.GetResultImage(); if (result != null) { TaskMetadata metadata = new TaskMetadata(result); if (form.IsImageModified) { AllowAnnotation = false; } if (form.Result == RegionResult.Region) { WindowInfo windowInfo = form.GetWindowInfo(); metadata.UpdateInfo(windowInfo); } lastRegionCaptureType = RegionCaptureType.Default; return(metadata); } } return(null); }