using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsApplication19
{
internal class Desktop
{
private const int SRCCOPY = 0x00CC0020;
#region Dll Imports
[DllImport("user32.dll", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=false)]
static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=false)]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=false)]
private static extern int ReleaseDC(IntPtr hwnd, IntPtr dc);
[DllImport("gdi32.dll", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=false)]
private static extern UInt64 BitBlt
(IntPtr hDestDC, int x, int y, int nWidth, int nHeight,
IntPtr hSrcDC, int xSrc, int ySrc, System.Int32 dwRop);
#endregion
private Desktop(){}
internal static Image GetBitmap(int x, int y, int width, int height)
{
Graphics desktopGraphics;
Image desktopImage;
Graphics drawGraphics;
Image drawImage;
Rectangle virtualScreen = SystemInformation.VirtualScreen;
using(desktopImage = new Bitmap(virtualScreen.Width, virtualScreen.Height))
{
using(desktopGraphics = Graphics.FromImage(desktopImage))
{
IntPtr ptrDesktop = desktopGraphics.GetHdc();
IntPtr ptrDesktopWindow = GetDesktopWindow();
IntPtr ptrWindowDC = GetWindowDC(ptrDesktopWindow);
BitBlt(ptrDesktop,
0,
0,
virtualScreen.Width,
virtualScreen.Height,
ptrWindowDC,
virtualScreen.X,
virtualScreen.Y,
SRCCOPY);
ReleaseDC(ptrDesktopWindow, ptrWindowDC);
desktopGraphics.ReleaseHdc(ptrDesktop);
ptrDesktop = IntPtr.Zero;
ptrDesktopWindow = IntPtr.Zero;
ptrWindowDC = IntPtr.Zero;
}
drawImage = new Bitmap(width, height);
using(drawGraphics = Graphics.FromImage(drawImage))
{
drawGraphics.DrawImage(
desktopImage,
new Rectangle(new Point(0,0), new Size(width, height)),
new Rectangle((virtualScreen.X - x) *-1, (virtualScreen.Y - y) *-1, width, height),
GraphicsUnit.Pixel);
}
}
return drawImage;
}
}
}
|