C# 获取桌面
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | [System.Runtime.InteropServices.DllImport(”gdi32.dll”)] public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, UInt32 dwRop); //创建桌面句柄 [System.Runtime.InteropServices.DllImportAttribute(”gdi32.dll”)] public static extern IntPtr CreateDC( string lpszDriver, string lpszDevice, string lpszOutput, int lpInitData); [System.Runtime.InteropServices.DllImport(”gdi32.dll”)] public static extern IntPtr CreateCompatibleDC(IntPtr hdc); //转换为本地的图像资源 [System.Runtime.InteropServices.DllImport(”gdi32.dll”)] public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); [System.Runtime.InteropServices.DllImport(”gdi32.dll”)] public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); [System.Runtime.InteropServices.DllImport(”gdi32.dll”)] public static extern int DeleteDC(IntPtr hdc); //释放用过的设备句柄 [DllImport(”user32.dll”)] public static extern bool ReleaseDC( IntPtr hwnd, IntPtr hdc ); //释放用过的画笔等资源 [DllImport(”gdi32.dll”)] public static extern bool DeleteObject( IntPtr hdc ); /// <summary> /// 截取屏幕图像 /// </summary> /// <param name=”Width”>宽</param> /// <param name=”Height”>高</param> /// <param name=”x”>x坐标(全屏时候为0)</param> /// <param name=”y”>y坐标(全屏时候为0)</param> /// <returns></returns> public Bitmap fullphoto( int Width, int Height, int x, int y) { Bitmap bitmap; //try //{ IntPtr hScreenDc = CreateDC(”DISPLAY”, null , null , 0); // 创建桌面句柄 IntPtr hMemDc = CreateCompatibleDC(hScreenDc); // 创建与桌面句柄相关连的内存DC IntPtr hBitmap = CreateCompatibleBitmap(hScreenDc, Width, Height); IntPtr hOldBitmap = SelectObject(hMemDc, hBitmap); BitBlt(hMemDc, x, y, Width, Height, hScreenDc, x, y, (UInt32)0xcc0020); IntPtr map = SelectObject(hMemDc, hOldBitmap); bitmap = Bitmap.FromHbitmap(map); ReleaseDC(hBitmap, hScreenDc); DeleteDC(hScreenDc); //删除用过的对象 DeleteDC(hMemDc); //删除用过的对象 DeleteDC(hOldBitmap); DeleteObject(hBitmap); //} //catch (Exception wx) //{ // return null; //} // number= number +1; // bitmap.Save(”screen” + number + “.bmp”); return bitmap; } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步