Created
October 7, 2018 20:25
-
-
Save progress44/9521b2e82490710620c501df6013c3a0 to your computer and use it in GitHub Desktop.
Taking screenshots on windows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class ScreenCapture | |
| { | |
| public Image CaptureScreen() | |
| { | |
| return CaptureWindow(User32.GetDesktopWindow()); | |
| } | |
| public Image CaptureWindow(IntPtr handle) | |
| { | |
| var screens = Screen.AllScreens; | |
| Bitmap shot; | |
| int height = 0; | |
| int width = 0; | |
| foreach (Screen sc in screens) | |
| { | |
| if (sc.Bounds.Height > height) | |
| { | |
| height = sc.Bounds.Height; | |
| } | |
| width += sc.Bounds.Width; | |
| } | |
| shot = new Bitmap(width, height, PixelFormat.Format32bppArgb); | |
| Graphics gfxScreenshot = Graphics.FromImage(shot); | |
| int x = 0; | |
| foreach (Screen sc in screens) | |
| { | |
| gfxScreenshot.CopyFromScreen( | |
| sc.Bounds.X, | |
| sc.Bounds.Y, | |
| x, | |
| 0, | |
| sc.Bounds.Size, | |
| CopyPixelOperation.SourceCopy); | |
| x += sc.Bounds.Width; | |
| } | |
| Image img = shot; | |
| return img; | |
| } | |
| public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format) | |
| { | |
| Image img = CaptureWindow(handle); | |
| img.Save(filename, format); | |
| } | |
| public void CaptureScreenToFile(string filename, ImageFormat format) | |
| { | |
| Image img = CaptureScreen(); | |
| img.Save(filename, format); | |
| } | |
| private class GDI32 | |
| { | |
| public const int SRCCOPY = 0x00CC0020; // BitBlt dwRop parameter | |
| [DllImport("gdi32.dll")] | |
| public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest, | |
| int nWidth, int nHeight, IntPtr hObjectSource, | |
| int nXSrc, int nYSrc, int dwRop); | |
| [DllImport("gdi32.dll")] | |
| public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth, | |
| int nHeight); | |
| [DllImport("gdi32.dll")] | |
| public static extern IntPtr CreateCompatibleDC(IntPtr hDC); | |
| [DllImport("gdi32.dll")] | |
| public static extern bool DeleteDC(IntPtr hDC); | |
| [DllImport("gdi32.dll")] | |
| public static extern bool DeleteObject(IntPtr hObject); | |
| [DllImport("gdi32.dll")] | |
| public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); | |
| } | |
| private class User32 | |
| { | |
| [StructLayout(LayoutKind.Sequential)] | |
| public struct RECT | |
| { | |
| public int left; | |
| public int top; | |
| public int right; | |
| public int bottom; | |
| } | |
| [DllImport("user32.dll")] | |
| public static extern IntPtr GetDesktopWindow(); | |
| [DllImport("user32.dll")] | |
| public static extern IntPtr GetWindowDC(IntPtr hWnd); | |
| [DllImport("user32.dll")] | |
| public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC); | |
| [DllImport("user32.dll")] | |
| public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect); | |
| } | |
| }//ScreenCapture |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment