public byte[] GetWindowBitmapUsingBitBlt(out int numBytesPerRow) { windowRect = new Win32Types.RECT(); bool result; if (isDesktop) { Win32Types.MonitorInfo mi = new Win32Types.MonitorInfo(); mi.cbSize = Marshal.SizeOf(mi); result = Win32Funcs.GetMonitorInfo(hwnd, ref mi); if (result) { windowRect = mi.rcMonitor; } } else { // Get the size of the window result = Win32Funcs.GetWindowRect(hwnd, out windowRect); } if (onlyCaptureMouse) { windowRect = cursorRect; result = true; } if (!result) { // Failed getting rect numBytesPerRow = 0; return(null); } // If they resized the window we need to reinit the memory if (windowWidth != windowRect.Width || windowHeight != windowRect.Height) { CleanupWindowCapture(); SetupWindowCapture(); windowWidth = windowRect.Width; windowHeight = windowRect.Height; } // Use the previously created device context with the bitmap Win32Funcs.SelectObject(hDest, curRenderingBitmap); if (onlyCaptureMouse) { if (isDesktop) { Win32Funcs.BitBlt(hDest, 0, 0, cursorRect.Width, cursorRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY); } else { Win32Funcs.BitBlt(hDest, cursorRect.Left, cursorRect.Height, cursorRect.Width, cursorRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY); } Win32Funcs.DrawIconEx(hDest, 1, 1, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL); } else { // Copy from the screen device context to the bitmap device context if (isDesktop) { Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY); } else { Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY); } Win32Funcs.DrawIconEx(hDest, cursorRect.Left, cursorRect.Top, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL); } Win32Types.BITMAP bitmap = new Win32Types.BITMAP(); Win32Funcs.GetObjectBitmap(curRenderingBitmap, Marshal.SizeOf(bitmap), ref bitmap); numBytesPerRow = bitmap.bmWidthBytes; if (bitmapBytes == null || bitmapBytes.Length != bitmap.bmHeight * bitmap.bmWidthBytes) { bitmapBytes = new byte[bitmap.bmHeight * bitmap.bmWidthBytes]; } if (bitmap.bmBits != IntPtr.Zero) { Marshal.Copy(bitmap.bmBits, bitmapBytes, 0, bitmapBytes.Length); } if (bitmapBytes == null || bitmapBytes.Length == 0) { return(null); } return(bitmapBytes); }
public static extern int GetObjectBitmap(IntPtr hObject, int nCount, ref Win32Types.BITMAP lpObject);
public byte[] GetWindowBitmapUsingBitBlt(out int numBytesPerRow) { windowRect = new Win32Types.RECT(); bool result; if (isDesktop) { Win32Types.MonitorInfo mi = new Win32Types.MonitorInfo(); mi.cbSize = Marshal.SizeOf(mi); result = Win32Funcs.GetMonitorInfo(hwnd, ref mi); if (result) { windowRect = mi.rcMonitor; } } else { // Get the size of the window result = Win32Funcs.GetWindowRect(hwnd, out windowRect); } if (onlyCaptureMouse) { windowRect = cursorRect; result = true; } if (!result) { // Failed getting rect numBytesPerRow = 0; return(null); } // If they resized the window we need to reinit the memory if (windowWidth != windowRect.Width || windowHeight != windowRect.Height) { CleanupWindowCapture(); SetupWindowCapture(); windowWidth = windowRect.Width; windowHeight = windowRect.Height; } oldhDest = Win32Funcs.SelectObject(hDest, curRenderingBitmap); Win32Types.BITMAP bitmap = new Win32Types.BITMAP(); Win32Funcs.GetObjectBitmap(curRenderingBitmap, Marshal.SizeOf(bitmap), ref bitmap); if (onlyCaptureMouse) { if (isDesktop) { Win32Funcs.BitBlt(hDest, 0, 0, cursorRect.Width, cursorRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY); } else { Win32Funcs.BitBlt(hDest, cursorRect.Left, cursorRect.Height, cursorRect.Width, cursorRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY); } Win32Funcs.DrawIconEx(hDest, 1, 1, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL); } else { // Copy from the screen device context to the bitmap device context var testing = false; if (isDesktop) { Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, windowRect.Left, windowRect.Top, Win32Consts.TernaryRasterOperations.SRCCOPY); } else { if (testing) { Win32Funcs.BitBlt(hDest, 0, 0, windowRect.Width, windowRect.Height, hdc, 0, 0, Win32Consts.TernaryRasterOperations.SRCCOPY); } else { if (needsGDIp) { Bitmap map = new Bitmap(windowRect.Width, windowRect.Height); Graphics graphics = Graphics.FromHdc(hDest); try { graphics.CopyFromScreen(new System.Drawing.Point(windowRect.Left, windowRect.Top), System.Drawing.Point.Empty, windowRect.Size, CopyPixelOperation.SourceCopy); } finally { map.Dispose(); graphics.Dispose(); } } } } Win32Funcs.DrawIconEx(hDest, cursorRect.Left, cursorRect.Top, cursorHandle, cursorRect.Width, cursorRect.Height, 0, IntPtr.Zero, Win32Consts.DI_NORMAL); } numBytesPerRow = bitmap.bmWidthBytes; if (bitmapBytes == null || bitmapBytes.Length != bitmap.bmHeight * bitmap.bmWidthBytes) { bitmapBytes = new byte[bitmap.bmHeight * bitmap.bmWidthBytes]; } if (bitmap.bmBits != IntPtr.Zero) { Marshal.Copy(bitmap.bmBits, bitmapBytes, 0, bitmapBytes.Length); } if (bitmapBytes == null || bitmapBytes.Length == 0) { return(null); } Win32Funcs.DeleteObject(bitmap.bmBits); Win32Funcs.SelectObject(hDest, oldhDest); return(bitmapBytes); }