晒一下c#截屏对象code
昨天,因为需要一个截屏的功能所以自己就写了一下。第一版是一个单纯的利用.net类库来实现的屏幕方式。
static public void PrintSystemScreen (string file) {
if (string.IsNullOrEmpty(file)) {
// Create a random file name by GUID and set the saved
// directory at current directory.
file = Guid.NewGuid().ToString();
}
// Create a bitmap for save
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppPArgb);
// Create a graphic for drawing from bmp
Graphics screenG = Graphics.FromImage(bmp);
screenG.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
ImageParams imgParam = new ImageParams(file);
bmp.Save(imgParam.FilePath, imgParam.CodeInfo, imgParam.EncoderParam);
}
if (string.IsNullOrEmpty(file)) {
// Create a random file name by GUID and set the saved
// directory at current directory.
file = Guid.NewGuid().ToString();
}
// Create a bitmap for save
Bitmap bmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppPArgb);
// Create a graphic for drawing from bmp
Graphics screenG = Graphics.FromImage(bmp);
screenG.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
ImageParams imgParam = new ImageParams(file);
bmp.Save(imgParam.FilePath, imgParam.CodeInfo, imgParam.EncoderParam);
}
第二版,我加入了根据给出的应用程序主窗口去截取界面的功能,使用Win32 Api来实现:
/// <summary>
/// GeneralScreen include serval functions.
/// Capture single screenshot.
/// Capture application screenshot.
/// !Capture web page screenshot.
/// </summary>
static public class GeneralScreen {
/// <summary>
/// Capturing the current screen operation then save
/// image to the indicated file.
/// </summary>
/// <param name="file"></param>
static public void PrintSystemScreen (string file) {
IntPtr screenHandle = NativeMethods.GetDesktopWindow();
Capture(screenHandle, file);
}
/// <summary>
/// Create a random file name by GUID under the runtime directory.
/// </summary>
static public void PrintSystemScreen () {
PrintSystemScreen(null);
}
/// <summary>
/// Capturing the indicated application window operation then save
/// image to the indicated file. If the file parameter not set, function will
/// create a random file name by GUID under the runtime directory.
/// </summary>
/// <param name="handle">window's handle</param>
/// <param name="file">target image file</param>
static public void PrintApplicationScreen (IntPtr handle, string file) {
if (NativeMethods.SetForegroundWindow(handle)) {
Capture(handle, file);
}
}
Helper
}
/// <summary>
/// The nativeMethods includes the win32 api for the others
/// class calling.
/// </summary>
internal static class NativeMethods {
// Methods
[DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern int BitBlt (IntPtr destDC, int xDest, int yDest, int width, int height, IntPtr sourceDC, int xSource, int ySource, uint rasterOperation);
[DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr CreateCompatibleBitmap (IntPtr dc, int width, int height);
[DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr CreateCompatibleDC (IntPtr dc);
[DllImport("gdi32.dll", EntryPoint = "CreateDCW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr CreateDC (string driver, IntPtr device, IntPtr output, IntPtr devMode);
[DllImport("gdi32.dll", ExactSpelling = true)]
internal static extern int DeleteDC (IntPtr dc);
[DllImport("gdi32.dll", ExactSpelling = true)]
internal static extern int DeleteObject (IntPtr gdiObject);
[DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr SelectObject (IntPtr dc, IntPtr gdi);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowRect (IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetForegroundWindow (IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow ();
}
在这里我用了一个辅助的类ImageParam,这个类可以根据给出的文件名自己识别需要生成什么格式的图片文件。/// GeneralScreen include serval functions.
/// Capture single screenshot.
/// Capture application screenshot.
/// !Capture web page screenshot.
/// </summary>
static public class GeneralScreen {
/// <summary>
/// Capturing the current screen operation then save
/// image to the indicated file.
/// </summary>
/// <param name="file"></param>
static public void PrintSystemScreen (string file) {
IntPtr screenHandle = NativeMethods.GetDesktopWindow();
Capture(screenHandle, file);
}
/// <summary>
/// Create a random file name by GUID under the runtime directory.
/// </summary>
static public void PrintSystemScreen () {
PrintSystemScreen(null);
}
/// <summary>
/// Capturing the indicated application window operation then save
/// image to the indicated file. If the file parameter not set, function will
/// create a random file name by GUID under the runtime directory.
/// </summary>
/// <param name="handle">window's handle</param>
/// <param name="file">target image file</param>
static public void PrintApplicationScreen (IntPtr handle, string file) {
if (NativeMethods.SetForegroundWindow(handle)) {
Capture(handle, file);
}
}
Helper
}
/// <summary>
/// The nativeMethods includes the win32 api for the others
/// class calling.
/// </summary>
internal static class NativeMethods {
// Methods
[DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern int BitBlt (IntPtr destDC, int xDest, int yDest, int width, int height, IntPtr sourceDC, int xSource, int ySource, uint rasterOperation);
[DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr CreateCompatibleBitmap (IntPtr dc, int width, int height);
[DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr CreateCompatibleDC (IntPtr dc);
[DllImport("gdi32.dll", EntryPoint = "CreateDCW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr CreateDC (string driver, IntPtr device, IntPtr output, IntPtr devMode);
[DllImport("gdi32.dll", ExactSpelling = true)]
internal static extern int DeleteDC (IntPtr dc);
[DllImport("gdi32.dll", ExactSpelling = true)]
internal static extern int DeleteObject (IntPtr gdiObject);
[DllImport("gdi32.dll", SetLastError = true, ExactSpelling = true)]
internal static extern IntPtr SelectObject (IntPtr dc, IntPtr gdi);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetWindowRect (IntPtr hWnd, out RECT lpRect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetForegroundWindow (IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow ();
}
完整的Demo工程从这里下载
将想法付诸于实践,借此来影响他人是一个人存在的真正价值