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
| int xLoc;
int yLoc;
IntPtr dsk;
IntPtr mem;
Bitmap currentView;
//get the handle of the desktop DC
dsk = Win32API.GetDC(Win32API.GetDesktopWindow());
//create memory DC
mem = Win32API.CreateCompatibleDC(dsk);
//get the X coordinates of the screen
xLoc = Win32API.GetSystemMetrics(SCREEN_X);
//get the Y coordinates of screen.
yLoc = Win32API.GetSystemMetrics(SCREEN_Y);
//create a compatible image the size of the desktop
newBMP = Win32API.CreateCompatibleBitmap(dsk, xLoc, yLoc);
//check against IntPtr (cant check IntPtr values against a null value)
if (newBMP != IntPtr.Zero)
{
//select the image in memory
IntPtr oldBmp = (IntPtr)Win32API.SelectObject(mem, newBMP);
//copy the new bitmap into memory
Win32API.BitBlt(mem, 0, 0, xLoc, yLoc, dsk, 0, 0, SRCCOPY);
//select the old bitmap into memory
Win32API.SelectObject(mem, oldBmp);
//delete the memoryDC since we're through with it
Win32API.DeleteDC(mem);
//release dskTopDC to free up the resources
Win32API.ReleaseDC(Win32API.GetDesktopWindow(), dsk);
//create out BitMap
currentView = Image.FromHbitmap(newBMP);
//return the image
return currentView;
} |
Partager