Making Screenshot class non static

This commit is contained in:
Jaex 2016-07-21 17:23:45 +03:00
parent f5ec0e0b8e
commit c57c643d79
14 changed files with 107 additions and 85 deletions

View file

@ -107,10 +107,15 @@ private void InitializeComponent()
ResumeLayout(false);
}
/// <summary>Must be called before show form</summary>
public virtual void Prepare()
public void Prepare()
{
backgroundImage = Screenshot.CaptureFullscreen();
Prepare(new Screenshot());
}
/// <summary>Must be called before show form</summary>
public virtual void Prepare(Screenshot screenshot)
{
backgroundImage = screenshot.CaptureFullscreen();
if (Config.UseDimming)
{

View file

@ -72,11 +72,11 @@ public class RectangleRegionAnnotateForm : Form
private Stopwatch penTimer;
private Font infoFont;
public RectangleRegionAnnotateForm(RectangleAnnotateOptions options)
public RectangleRegionAnnotateForm(Screenshot screenshot, RectangleAnnotateOptions options)
{
Options = options;
backgroundImage = Screenshot.CaptureFullscreen();
backgroundImage = screenshot.CaptureFullscreen();
borderDotPen = new Pen(Color.Black, 1);
borderDotPen2 = new Pen(Color.White, 1);
borderDotPen2.DashPattern = new float[] { 5, 5 };

View file

@ -122,9 +122,9 @@ private void CopyAreaInfo()
ClipboardHelpers.CopyText(clipboardText);
}
public override void Prepare()
public override void Prepare(Screenshot screenshot)
{
base.Prepare();
base.Prepare(screenshot);
if (Config != null)
{

View file

@ -57,9 +57,9 @@ public class RectangleRegionLightForm : Form
private bool isMouseDown;
private Stopwatch penTimer;
public RectangleRegionLightForm()
public RectangleRegionLightForm(Screenshot screenshot)
{
backgroundImage = Screenshot.CaptureFullscreen();
backgroundImage = screenshot.CaptureFullscreen();
backgroundBrush = new TextureBrush(backgroundImage);
borderDotPen = new Pen(Color.Black, 1);
borderDotPen2 = new Pen(Color.White, 1);

View file

@ -158,13 +158,13 @@ private void RectangleTransparent_MouseUp(object sender, MouseEventArgs e)
}
}
public Image GetAreaImage()
public Image GetAreaImage(Screenshot screenshot)
{
Rectangle rect = SelectionRectangle0Based;
if (rect.Width > 0 && rect.Height > 0)
{
return Screenshot.CaptureRectangle(SelectionRectangle);
return screenshot.CaptureRectangle(SelectionRectangle);
}
return null;

View file

@ -313,8 +313,8 @@ private void captureTimer_Tick(object sender, EventArgs e)
}
}
Screenshot.CaptureCursor = false;
Image image = Screenshot.CaptureRectangle(selectedRectangle);
Screenshot screenshot = new Screenshot() { CaptureCursor = false };
Image image = screenshot.CaptureRectangle(selectedRectangle);
if (image != null)
{

View file

@ -96,12 +96,13 @@ private set
private int fps, delay, frameCount, previousProgress;
private float durationSeconds;
private Screenshot screenshot;
private Rectangle captureRectangle;
private ImageCache imgCache;
private FFmpegHelper ffmpegCli;
private bool stopRequest;
public ScreenRecorder(ScreenRecordOutput outputType, ScreencastOptions options, Rectangle captureRectangle)
public ScreenRecorder(ScreenRecordOutput outputType, ScreencastOptions options, Screenshot screenshot, Rectangle captureRectangle)
{
if (string.IsNullOrEmpty(options.OutputPath))
{
@ -127,6 +128,8 @@ public ScreenRecorder(ScreenRecordOutput outputType, ScreencastOptions options,
imgCache = new HardDiskCache(Options);
break;
}
this.screenshot = screenshot;
}
private void UpdateInfo()
@ -164,7 +167,7 @@ private void RecordUsingCache()
{
Stopwatch timer = Stopwatch.StartNew();
Image img = Screenshot.CaptureRectangle(CaptureRectangle);
Image img = screenshot.CaptureRectangle(CaptureRectangle);
//DebugHelper.WriteLine("Screen capture: " + (int)timer.ElapsedMilliseconds);
imgCache.AddImageAsync(img);

View file

@ -30,16 +30,16 @@
namespace ShareX.ScreenCaptureLib
{
public static partial class Screenshot
public partial class Screenshot
{
public static bool RemoveOutsideScreenArea = true;
public static bool CaptureCursor = false;
public static bool CaptureClientArea = false;
public static bool CaptureShadow = true;
public static int ShadowOffset = 20;
public static bool AutoHideTaskbar = false;
public bool CaptureCursor { get; set; } = false;
public bool CaptureClientArea { get; set; } = false;
public bool RemoveOutsideScreenArea { get; set; } = true;
public bool CaptureShadow { get; set; } = false;
public int ShadowOffset { get; set; } = 20;
public bool AutoHideTaskbar { get; set; } = false;
public static Image CaptureRectangle(Rectangle rect)
public Image CaptureRectangle(Rectangle rect)
{
if (RemoveOutsideScreenArea)
{
@ -50,14 +50,14 @@ public static Image CaptureRectangle(Rectangle rect)
return CaptureRectangleNative(rect, CaptureCursor);
}
public static Image CaptureFullscreen()
public Image CaptureFullscreen()
{
Rectangle bounds = CaptureHelpers.GetScreenBounds();
return CaptureRectangle(bounds);
}
public static Image CaptureWindow(IntPtr handle)
public Image CaptureWindow(IntPtr handle)
{
if (handle.ToInt32() > 0)
{
@ -95,26 +95,26 @@ public static Image CaptureWindow(IntPtr handle)
return null;
}
public static Image CaptureActiveWindow()
public Image CaptureActiveWindow()
{
IntPtr handle = NativeMethods.GetForegroundWindow();
return CaptureWindow(handle);
}
public static Image CaptureActiveMonitor()
public Image CaptureActiveMonitor()
{
Rectangle bounds = CaptureHelpers.GetActiveScreenBounds();
return CaptureRectangle(bounds);
}
public static Image CaptureRectangleNative(Rectangle rect, bool captureCursor = false)
private Image CaptureRectangleNative(Rectangle rect, bool captureCursor = false)
{
return CaptureRectangleNative(NativeMethods.GetDesktopWindow(), rect, captureCursor);
}
public static Image CaptureRectangleNative(IntPtr handle, Rectangle rect, bool captureCursor = false)
private Image CaptureRectangleNative(IntPtr handle, Rectangle rect, bool captureCursor = false)
{
if (rect.Width == 0 || rect.Height == 0)
{
@ -153,7 +153,7 @@ public static Image CaptureRectangleNative(IntPtr handle, Rectangle rect, bool c
return img;
}
public static Image CaptureRectangleManaged(Rectangle rect)
private Image CaptureRectangleManaged(Rectangle rect)
{
if (rect.Width == 0 || rect.Height == 0)
{

View file

@ -32,9 +32,9 @@
namespace ShareX.ScreenCaptureLib
{
public static partial class Screenshot
public partial class Screenshot
{
public static Image CaptureWindowTransparent(IntPtr handle)
public Image CaptureWindowTransparent(IntPtr handle)
{
if (handle.ToInt32() > 0)
{
@ -154,14 +154,14 @@ public static Image CaptureWindowTransparent(IntPtr handle)
return null;
}
public static Image CaptureActiveWindowTransparent()
public Image CaptureActiveWindowTransparent()
{
IntPtr handle = NativeMethods.GetForegroundWindow();
return CaptureWindowTransparent(handle);
}
private static Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
private Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
{
if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size)
{
@ -202,7 +202,7 @@ private static Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blac
return whiteBackground;
}
private static Bitmap TrimTransparent(Bitmap bitmap)
private Bitmap TrimTransparent(Bitmap bitmap)
{
Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
Rectangle rect = source;
@ -229,7 +229,7 @@ private static Bitmap TrimTransparent(Bitmap bitmap)
return bitmap;
}
private static Rectangle TrimTransparentFindX(UnsafeBitmap unsafeBitmap, Rectangle rect)
private Rectangle TrimTransparentFindX(UnsafeBitmap unsafeBitmap, Rectangle rect)
{
for (int x = rect.X; x < rect.Width; x++)
{
@ -246,7 +246,7 @@ private static Rectangle TrimTransparentFindX(UnsafeBitmap unsafeBitmap, Rectang
return rect;
}
private static Rectangle TrimTransparentFindY(UnsafeBitmap unsafeBitmap, Rectangle rect)
private Rectangle TrimTransparentFindY(UnsafeBitmap unsafeBitmap, Rectangle rect)
{
for (int y = rect.Y; y < rect.Height; y++)
{
@ -263,7 +263,7 @@ private static Rectangle TrimTransparentFindY(UnsafeBitmap unsafeBitmap, Rectang
return rect;
}
private static Rectangle TrimTransparentFindWidth(UnsafeBitmap unsafeBitmap, Rectangle rect)
private Rectangle TrimTransparentFindWidth(UnsafeBitmap unsafeBitmap, Rectangle rect)
{
for (int x = rect.Width - 1; x >= rect.X; x--)
{
@ -280,7 +280,7 @@ private static Rectangle TrimTransparentFindWidth(UnsafeBitmap unsafeBitmap, Rec
return rect;
}
private static Rectangle TrimTransparentFindHeight(UnsafeBitmap unsafeBitmap, Rectangle rect)
private Rectangle TrimTransparentFindHeight(UnsafeBitmap unsafeBitmap, Rectangle rect)
{
for (int y = rect.Height - 1; y >= rect.Y; y--)
{
@ -297,7 +297,7 @@ private static Rectangle TrimTransparentFindHeight(UnsafeBitmap unsafeBitmap, Re
return rect;
}
private static Bitmap QuickTrimTransparent(Bitmap bitmap)
private Bitmap QuickTrimTransparent(Bitmap bitmap)
{
Rectangle source = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
Rectangle rect = source;
@ -356,7 +356,7 @@ private static Bitmap QuickTrimTransparent(Bitmap bitmap)
return bitmap;
}
private static void TrimShadow(Bitmap bitmap)
private void TrimShadow(Bitmap bitmap)
{
int sizeLimit = 10;
int alphaLimit = 200;
@ -427,7 +427,7 @@ private static void TrimShadow(Bitmap bitmap)
#region Not in use
private static byte[,] windows7Corner = new byte[,]
private byte[,] windows7Corner = new byte[,]
{
{ 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 }, { 4, 0 },
{ 0, 1 }, { 1, 1 }, { 2, 1 },
@ -436,7 +436,7 @@ private static void TrimShadow(Bitmap bitmap)
{ 0, 4 }
};
private static byte[,] windowsVistaCorner = new byte[,]
private byte[,] windowsVistaCorner = new byte[,]
{
{ 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
{ 0, 1 }, { 1, 1 },
@ -444,7 +444,7 @@ private static void TrimShadow(Bitmap bitmap)
{ 0, 3 }
};
private static Bitmap RemoveCorners(Image img)
private Bitmap RemoveCorners(Image img)
{
byte[,] corner;
@ -464,7 +464,7 @@ private static Bitmap RemoveCorners(Image img)
return RemoveCorners(img, corner);
}
private static Bitmap RemoveCorners(Image img, byte[,] cornerData)
private Bitmap RemoveCorners(Image img, byte[,] cornerData)
{
Bitmap bmp = new Bitmap(img);

View file

@ -744,7 +744,7 @@ private void cbDontShowPrintSettingDialog_CheckedChanged(object sender, EventArg
private void btnShowImagePrintSettings_Click(object sender, EventArgs e)
{
using (Image testImage = Screenshot.CaptureActiveMonitor())
using (Image testImage = TaskHelpers.GetScreenshot().CaptureActiveMonitor())
using (PrintForm printForm = new PrintForm(testImage, Program.Settings.PrintSettings, true))
{
printForm.ShowDialog();

View file

@ -107,11 +107,12 @@ private void TakeScreenshot()
if (!rect.IsEmpty)
{
Image img = Screenshot.CaptureRectangle(rect);
TaskSettings taskSettings = TaskSettings.GetDefaultTaskSettings();
Image img = TaskHelpers.GetScreenshot(taskSettings).CaptureRectangle(rect);
if (img != null)
{
TaskSettings taskSettings = TaskSettings.GetDefaultTaskSettings();
taskSettings.UseDefaultAfterCaptureJob = false;
taskSettings.AfterCaptureJob = taskSettings.AfterCaptureJob.Remove(AfterCaptureTasks.AnnotateImage);
taskSettings.UseDefaultAdvancedSettings = false;

View file

@ -1964,13 +1964,13 @@ public void CaptureScreenshot(CaptureType captureType, TaskSettings taskSettings
switch (captureType)
{
case CaptureType.Screen:
DoCapture(Screenshot.CaptureFullscreen, CaptureType.Screen, taskSettings, autoHideForm);
DoCapture(TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen, CaptureType.Screen, taskSettings, autoHideForm);
break;
case CaptureType.ActiveWindow:
CaptureActiveWindow(taskSettings, autoHideForm);
break;
case CaptureType.ActiveMonitor:
DoCapture(Screenshot.CaptureActiveMonitor, CaptureType.ActiveMonitor, taskSettings, autoHideForm);
DoCapture(TaskHelpers.GetScreenshot(taskSettings).CaptureActiveMonitor, CaptureType.ActiveMonitor, taskSettings, autoHideForm);
break;
case CaptureType.Rectangle:
case CaptureType.Polygon:
@ -2020,12 +2020,6 @@ private void DoCaptureWork(ScreenCaptureDelegate capture, CaptureType captureTyp
try
{
Screenshot.CaptureCursor = taskSettings.CaptureSettings.ShowCursor;
Screenshot.CaptureShadow = taskSettings.CaptureSettings.CaptureShadow;
Screenshot.ShadowOffset = taskSettings.CaptureSettings.CaptureShadowOffset;
Screenshot.CaptureClientArea = taskSettings.CaptureSettings.CaptureClientArea;
Screenshot.AutoHideTaskbar = taskSettings.CaptureSettings.CaptureAutoHideTaskbar;
img = capture();
}
catch (Exception ex)
@ -2084,11 +2078,11 @@ private void CaptureActiveWindow(TaskSettings taskSettings, bool autoHideForm =
if (taskSettings.CaptureSettings.CaptureTransparent && !taskSettings.CaptureSettings.CaptureClientArea)
{
img = Screenshot.CaptureActiveWindowTransparent();
img = TaskHelpers.GetScreenshot(taskSettings).CaptureActiveWindowTransparent();
}
else
{
img = Screenshot.CaptureActiveWindow();
img = TaskHelpers.GetScreenshot(taskSettings).CaptureActiveWindow();
}
img.Tag = new ImageTag
@ -2106,7 +2100,7 @@ private void CaptureCustomRegion(TaskSettings taskSettings, bool autoHideForm)
DoCapture(() =>
{
Rectangle regionBounds = taskSettings.CaptureSettings.CaptureCustomRegion;
Image img = Screenshot.CaptureRectangle(regionBounds);
Image img = TaskHelpers.GetScreenshot(taskSettings).CaptureRectangle(regionBounds);
return img;
}, CaptureType.CustomRegion, taskSettings, autoHideForm);
@ -2130,10 +2124,10 @@ private void CaptureWindow(IntPtr handle, TaskSettings taskSettings = null, bool
if (taskSettings.CaptureSettings.CaptureTransparent && !taskSettings.CaptureSettings.CaptureClientArea)
{
return Screenshot.CaptureWindowTransparent(handle);
return TaskHelpers.GetScreenshot(taskSettings).CaptureWindowTransparent(handle);
}
return Screenshot.CaptureWindow(handle);
return TaskHelpers.GetScreenshot(taskSettings).CaptureWindow(handle);
}, CaptureType.Window, taskSettings, autoHideForm);
}
@ -2162,7 +2156,7 @@ private void CaptureRegion(CaptureType captureType, TaskSettings taskSettings, b
try
{
form.Config = taskSettings.CaptureSettingsReference.SurfaceOptions;
form.Prepare();
form.Prepare(TaskHelpers.GetScreenshot(taskSettings));
form.ShowDialog();
img = form.GetResultImage();
@ -2206,7 +2200,7 @@ private void CaptureRectangleLight(TaskSettings taskSettings = null, bool autoHi
{
Image img = null;
using (RectangleRegionLightForm rectangleLight = new RectangleRegionLightForm())
using (RectangleRegionLightForm rectangleLight = new RectangleRegionLightForm(TaskHelpers.GetScreenshot(taskSettings)))
{
if (rectangleLight.ShowDialog() == DialogResult.OK)
{
@ -2235,7 +2229,7 @@ private void CaptureRectangleTransparent(TaskSettings taskSettings = null, bool
{
if (rectangleTransparent.ShowDialog() == DialogResult.OK)
{
img = rectangleTransparent.GetAreaImage();
img = rectangleTransparent.GetAreaImage(TaskHelpers.GetScreenshot(taskSettings));
if (img != null)
{
@ -2256,7 +2250,8 @@ private void CaptureRectangleAnnotate(TaskSettings taskSettings = null, bool aut
{
Image img = null;
using (RectangleRegionAnnotateForm rectangleAnnotate = new RectangleRegionAnnotateForm(taskSettings.CaptureSettingsReference.RectangleAnnotateOptions))
using (RectangleRegionAnnotateForm rectangleAnnotate = new RectangleRegionAnnotateForm(TaskHelpers.GetScreenshot(taskSettings),
taskSettings.CaptureSettingsReference.RectangleAnnotateOptions))
{
if (rectangleAnnotate.ShowDialog() == DialogResult.OK)
{
@ -2282,7 +2277,7 @@ private void CaptureLastRegion(TaskSettings taskSettings, bool autoHideForm = tr
{
DoCapture(() =>
{
using (Image screenshot = Screenshot.CaptureFullscreen())
using (Image screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen())
{
return RegionCaptureHelpers.ApplyRegionPathToImage(screenshot, BaseRegionForm.LastRegionFillPath, taskSettings.CaptureSettings.SurfaceOptions);
}
@ -2298,7 +2293,7 @@ private void CaptureLastRegion(TaskSettings taskSettings, bool autoHideForm = tr
{
DoCapture(() =>
{
using (Image screenshot = Screenshot.CaptureFullscreen())
using (Image screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen())
{
return ImageHelpers.CropImage(screenshot, RectangleRegionLightForm.LastSelectionRectangle0Based);
}
@ -2314,7 +2309,7 @@ private void CaptureLastRegion(TaskSettings taskSettings, bool autoHideForm = tr
{
DoCapture(() =>
{
using (Image screenshot = Screenshot.CaptureFullscreen())
using (Image screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen())
{
return ImageHelpers.CropImage(screenshot, RectangleRegionTransparentForm.LastSelectionRectangle0Based);
}
@ -2330,7 +2325,7 @@ private void CaptureLastRegion(TaskSettings taskSettings, bool autoHideForm = tr
{
DoCapture(() =>
{
using (Image screenshot = Screenshot.CaptureFullscreen())
using (Image screenshot = TaskHelpers.GetScreenshot(taskSettings).CaptureFullscreen())
{
return ImageHelpers.CropImage(screenshot, RectangleRegionAnnotateForm.LastSelectionRectangle0Based);
}
@ -2429,7 +2424,7 @@ private void tsmiMonitorItems_Click(object sender, EventArgs e)
Rectangle rectangle = (Rectangle)tsi.Tag;
if (!rectangle.IsEmpty)
{
DoCapture(() => Screenshot.CaptureRectangle(rectangle), CaptureType.Monitor);
DoCapture(() => TaskHelpers.GetScreenshot().CaptureRectangle(rectangle), CaptureType.Monitor);
}
}
@ -2502,7 +2497,7 @@ private void tsmiTrayMonitorItems_Click(object sender, EventArgs e)
Rectangle rectangle = (Rectangle)tsi.Tag;
if (!rectangle.IsEmpty)
{
DoCapture(() => Screenshot.CaptureRectangle(rectangle), CaptureType.Monitor, null, false);
DoCapture(() => TaskHelpers.GetScreenshot().CaptureRectangle(rectangle), CaptureType.Monitor, null, false);
}
}

View file

@ -157,8 +157,6 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
IsRecording = true;
Screenshot.CaptureCursor = taskSettings.CaptureSettings.ScreenRecordShowCursor;
string path = "";
bool abortRequested = false;
@ -181,17 +179,6 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
path = Program.ScreenRecorderCacheFilePath;
}
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
};
recordForm.ChangeState(ScreenRecordState.BeforeStart);
if (taskSettings.CaptureSettings.ScreenRecordAutoStart)
@ -217,7 +204,21 @@ private static void StartRecording(ScreenRecordOutput outputType, TaskSettings t
if (!abortRequested)
{
screenRecorder = new ScreenRecorder(outputType, options, captureRectangle);
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(outputType, options, screenshot, captureRectangle);
screenRecorder.RecordingStarted += () => recordForm.ChangeState(ScreenRecordState.AfterRecordingStart);
recordForm.ChangeState(ScreenRecordState.AfterStart);
screenRecorder.StartRecording();

View file

@ -989,5 +989,22 @@ public static Image FindMenuIcon<T>(int index)
return null;
}
public static Screenshot GetScreenshot(TaskSettings taskSettings = null)
{
if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings();
Screenshot screenshot = new Screenshot()
{
CaptureCursor = taskSettings.CaptureSettings.ShowCursor,
CaptureClientArea = taskSettings.CaptureSettings.CaptureClientArea,
RemoveOutsideScreenArea = true,
CaptureShadow = taskSettings.CaptureSettings.CaptureShadow,
ShadowOffset = taskSettings.CaptureSettings.CaptureShadowOffset,
AutoHideTaskbar = taskSettings.CaptureSettings.CaptureAutoHideTaskbar
};
return screenshot;
}
}
}