Files
VAR.Toolbox/VAR.ScreenAutomation/Code/Screenshoter.cs

51 lines
1.7 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
namespace VAR.ScreenAutomation.Code
{
public static class Screenshoter
{
public static Bitmap CaptureControl(Control ctrl, Bitmap bmp = null)
{
if (ctrl == null) { return bmp; }
Point picCapturerOrigin = ctrl.PointToScreen(new Point(0, 0));
bmp = CaptureScreen(bmp, picCapturerOrigin.X, picCapturerOrigin.Y, ctrl.Width, ctrl.Height);
return bmp;
}
private static Bitmap CaptureScreen(Bitmap bmp = null, int? left = null, int? top = null, int? width = null,
int? height = null)
{
if (width <= 0 || height <= 0) { return bmp; }
// Determine the size of the "virtual screen", which includes all monitors.
left = left ?? SystemInformation.VirtualScreen.Left;
top = top ?? SystemInformation.VirtualScreen.Top;
width = width ?? SystemInformation.VirtualScreen.Width;
height = height ?? SystemInformation.VirtualScreen.Height;
// Create a bitmap of the appropriate size to receive the screenshot.
if (bmp == null || bmp.Width != width || bmp.Height != height)
{
bmp = new Bitmap((int)width, (int)height);
}
try
{
// Draw the screenshot into our bitmap.
using (Graphics g = Graphics.FromImage(bmp))
{
g.CopyFromScreen((int)left, (int)top, 0, 0, bmp.Size);
}
}
catch (Exception)
{
/* Nom Nom Nom */
}
return bmp;
}
}
}