1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41:
| public class Capturer { [DllImport("coredll.dll")] public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
[DllImport("coredll.dll")] private static extern IntPtr GetDC(IntPtr hwnd);
const int SRCCOPY = 0x00CC0020;
[DllImport("gdi32.dll", SetLastError = true)] public static extern int StretchBlt(IntPtr hdcDest, int nXDest, int nYDest,int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXSrc, int nYSrc, int nWidthSrc, int nHeightSrc, uint dwRop);
public static void Snapshot(string fileName, Rectangle rectangle) { IntPtr deviceContext = GetDC(IntPtr.Zero); Bitmap newImage = new Bitmap(newImage.Width, newImage.Height);
using (Graphics deviceGraphics = Graphics.FromHdc(deviceContext)) using (Graphics captureGraphics = Graphics.FromImage(capture)) {
StretchBlt(captureGraphics.GetHdc(), 0, 0, 200, 100, deviceGraphics.GetHdc(), 70, 0, 570, 480, SRCCOPY); }
capture.Save(fileName, System.Drawing.Imaging.ImageFormat.Png); }
} |