DotNetCore Capture A Screenshot in Windows
System.Drawing.Common
is available cross platform in .NET Core now allowing you to take screenshots of windows/your desktop. Here is an example
EDIT
An example capturing a screenshot:
using var bitmap = new Bitmap(1920, 1080);
using (var g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(0, 0, 0, 0,
bitmap.Size, CopyPixelOperation.SourceCopy);
}
bitmap.Save("filename.jpg", ImageFormat.Jpeg);
How to capture a full website screenshot with C# and WebKit.NET?
Seems that it is kind of possible by using NativeMethods.SendMessage, although this can screw up the message queue, could you use http://cutycapt.sourceforge.net/ or perhaps http://iecapt.sourceforge.net/ or http://labs.awesomium.com/capturing-web-pages-with-c-net/?
Can I use ImageSharp with .Net Core for a screen capture of the web browser
ImageSharp is a 2D Graphics API. Screen Capture is an operation that will require an embedded web browser and falls well outside of the goals of the library.
C#/.Net code to screen capture multiple monitors with scaling
We needed a solution so I did some experimenting. First of all, we needed a C# class for some Windows methods. This code is stolen, not original.
class NativeUtilities
{
[Flags()]
public enum DisplayDeviceStateFlags : int
{
/// <summary>The device is part of the desktop.</summary>
AttachedToDesktop = 0x1,
MultiDriver = 0x2,
/// <summary>This is the primary display.</summary>
PrimaryDevice = 0x4,
/// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
MirroringDriver = 0x8,
/// <summary>The device is VGA compatible.</summary>
VGACompatible = 0x16,
/// <summary>The device is removable; it cannot be the primary display.</summary>
Removable = 0x20,
/// <summary>The device has more display modes than its output devices support.</summary>
ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DisplayDevice
{
[MarshalAs(UnmanagedType.U4)]
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
[MarshalAs(UnmanagedType.U4)]
public DisplayDeviceStateFlags StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
}
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
private const int CCHDEVICENAME = 0x20;
private const int CCHFORMNAME = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public ScreenOrientation dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(string deviceName, int modeNum, ref DEVMODE devMode);
public const int ENUM_CURRENT_SETTINGS = -1;
const int ENUM_REGISTRY_SETTINGS = -2;
[DllImport("User32.dll")]
public static extern int EnumDisplayDevices(string lpDevice, int iDevNum, ref DisplayDevice lpDisplayDevice, int dwFlags);
}
Then I wrote a method to call this code, using the above Windows methods, as opposed to the .Net methods we had been using:
public static void ScreenCapture(string filename)
{
// Initialize the virtual screen to dummy values
int screenLeft = int.MaxValue;
int screenTop = int.MaxValue;
int screenRight = int.MinValue;
int screenBottom = int.MinValue;
// Enumerate system display devices
int deviceIndex = 0;
while (true)
{
NativeUtilities.DisplayDevice deviceData = new NativeUtilities.DisplayDevice{cb = Marshal.SizeOf(typeof(NativeUtilities.DisplayDevice))};
if (NativeUtilities.EnumDisplayDevices(null, deviceIndex, ref deviceData, 0) != 0)
{
// Get the position and size of this particular display device
NativeUtilities.DEVMODE devMode = new NativeUtilities.DEVMODE();
if (NativeUtilities.EnumDisplaySettings(deviceData.DeviceName, NativeUtilities.ENUM_CURRENT_SETTINGS, ref devMode))
{
// Update the virtual screen dimensions
screenLeft = Math.Min(screenLeft, devMode.dmPositionX);
screenTop = Math.Min(screenTop, devMode.dmPositionY);
screenRight = Math.Max(screenRight, devMode.dmPositionX + devMode.dmPelsWidth);
screenBottom = Math.Max(screenBottom, devMode.dmPositionY + devMode.dmPelsHeight);
}
deviceIndex++;
}
else
break;
}
// Create a bitmap of the appropriate size to receive the screen-shot.
using (Bitmap bmp = new Bitmap(screenRight - screenLeft, screenBottom - screenTop))
{
// Draw the screen-shot into our bitmap.
using (Graphics g = Graphics.FromImage(bmp))
g.CopyFromScreen(screenLeft, screenTop, 0, 0, bmp.Size);
// Stuff the bitmap into a file
bmp.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
}
}
This works and has been pulled from a large application. I hope I've included all the necessary pieces.
Related Topics
Connect to Network Drive with User Name and Password
Instantiate an Object with a Runtime-Determined Type
Does List<T> Guarantee Insertion Order
Don't Stop Debugger at That Exception When It's Thrown and Caught
Passing List<> to SQL Stored Procedure
The Type Must Be a Reference Type in Order to Use It as Parameter 'T' in the Generic Type or Method
How to Accept an Array as an ASP.NET MVC Controller Action Parameter
How to Determine for Which Platform an Executable Is Compiled
Capture the Screen Shot Using .Net
Raise Event Thread Safely - Best Practice
X509Certificate Constructor Exception
ASP.NET MVC: How to Redirect a Non Www to Www and Vice Versa
Linq to Entityframework Datetime