|
|
发表于 2005-4-7 15:36:00
|
显示全部楼层
Re:windows GDI+的问题:如何得到一个windows屏幕图像?
需要使用的WIN API:
[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,Int64 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);
==========================================
c#代码:
IntPtr hScreenDc = CreateDC("DISPLAY",null,null,0); // 创建桌面句柄
IntPtr hMemDc = CreateCompatibleDC(hScreenDc); // 创建与桌面句柄相关连的内存DC
IntPtr hBitmap = CreateCompatibleBitmap(hScreenDc, 1024, 768);
IntPtr hOldBitmap = SelectObject( hMemDc, hBitmap);
BitBlt( hMemDc, 0, 0, 1024, 768, hScreenDc, 0, 0,(UInt32)0xcc0020);
hBitmap = SelectObject( hMemDc, hOldBitmap);
DeleteDC( hScreenDc);//删除用过的对象
DeleteDC( hMemDc);//删除用过的对象
Bitmap bitmap = Bitmap.FromHbitmap(hBitmap);
bitmap.Save("screen.bmp"); |
|