Screen Capture in .NET Compact Framework
Monday, March 31st, 2008
Five years ago, if you wanted to do a screen capture on Windows Mobile devices with .NET Compact Framework, you would look at Screen Capture, I did not count the lines, but looked quiet long. Five years later, I wanted to do the same thing, and my code looked like this:
public static Bitmap ScreenCapture(IntPtr hWnd, Rectangle rect) hBitmap = CoreDll.CreateCompatibleBitmap(hDC, rect.Width, rect.Height); if (hBitmap != IntPtr.Zero) CoreDll.SelectObject(hMemDC, hOld); CoreDll.DeleteDC(hMemDC); return null;
{
IntPtr hBitmap;
IntPtr hDC = CoreDll.GetDC(hWnd);
IntPtr hMemDC = CoreDll.CreateCompatibleDC(hDC);
{
IntPtr hOld = (IntPtr)CoreDll.SelectObject(hMemDC, hBitmap);
CoreDll.BitBlt(hMemDC, 0, 0, rect.Width, rect.Height, hDC, 0, 0, CoreDll.SRCCOPY);
CoreDll.ReleaseDC(hWnd, hDC);
Bitmap bmp = System.Drawing.Image.FromHbitmap(hBitmap);
CoreDll.DeleteObject(hBitmap);
return bmp;
}
}
The CoreDll imports:
[DllImport("Coredll.dll", EntryPoint = "GetDC")]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImportAttribute("coredll.dll", EntryPoint = "GetWindowDC")]
internal static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("Coredll.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
[DllImport("coredll.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);
[DllImport("coredll.dll")]
public static extern IntPtr CreateDIBSection(IntPtr hdc, IntPtr hdr, uint colors, ref IntPtr pBits, IntPtr hFile, uint offset);
[DllImport("coredll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
[DllImport("coredll")]
public static extern int DeleteDC(IntPtr hdc);
[System.Runtime.InteropServices.DllImportAttribute("coredll.dll", EntryPoint = "ReleaseDC")]
internal static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("coredll")]
public static extern IntPtr DeleteObject(IntPtr hObject);
This entry is filed under .NETCF, Mobile. You can follow any responses to this entry through the RSS 2.0 feed.You can leave a response, or trackback from your own site.