1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 using System.Runtime.InteropServices; 5 namespace ConsoleApplication1 6 { 7 class Program 8 { 9 //Win32 API 10 [DllImport("User32.dll")] 11 public static extern int GetSystemMetrics(int nIndex); 12 static void Main(string[] args) 13 { 14 int Width,Height; 15 double LineWidth, Size; 16 Width=GetSystemMetrics(0); 17 Height = GetSystemMetrics(1); 18 //验证 19 Console.WriteLine("{0}*{1}", Width, Height); 20 Console.ReadKey(); 21 LineWidth = (((Width * 24) + 31) & (0x7FFFFFFF - 31)) >> 3; //对齐后每行数据长度 22 Size = LineWidth * Height; 23 //验证 24 Console.WriteLine("{0};{1}", LineWidth, Size); 25 Console.ReadKey(); 26 GetMem(Bits, Size);//动态分布内存 27 //截图 28 Wnd=GetDesktopWindow(); //获取桌面句柄 29 DC = GetWindowDC(Wnd); //返回hWnd参数所指定的窗口的设备环境 30 MemDC = CreateCompatibleDC(DC); //该函数创建一个与指定设备兼容的内存设备上下文环境 31 Bitmap = CreateCompatibleBitmap(DC, Width, Height); //创建与指定的设备环境相关的设备兼容的位图 32 OldBitmap=SelectObject(MemDC,Bitmap); //选择一对象到指定的设备上下文环境中 33 BitBlt(MemDC,0,0,Width,Height,DC,0,0,SRCCOPY); // 34 Bitmap=SelectObject(MemDC,OldBitmap); 35 } 36 } 37 }