public BufferedImage capture(final HWND hWnd) { final HDC hdcWindow = User32.INSTANCE.GetDC(hWnd); final HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow); final RECT bounds = new RECT(); User32Extra.INSTANCE.GetClientRect(hWnd, bounds); final int width = bounds.right - bounds.left; final int height = bounds.bottom - bounds.top; if (width * height <= 0) { return null; } final HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height); final HANDLE hOld = GDI32.INSTANCE.SelectObject(hdcMemDC, hBitmap); GDI32Extra.INSTANCE.BitBlt(hdcMemDC, 0, 0, width, height, hdcWindow, 0, 0, WinGDIExtra.SRCCOPY); GDI32.INSTANCE.SelectObject(hdcMemDC, hOld); GDI32.INSTANCE.DeleteDC(hdcMemDC); final BITMAPINFO bmi = new BITMAPINFO(); bmi.bmiHeader.biWidth = width; bmi.bmiHeader.biHeight = -height; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = WinGDI.BI_RGB; final Memory buffer = new Memory(width * height * 4); GDI32.INSTANCE.GetDIBits(hdcWindow, hBitmap, 0, height, buffer, bmi, WinGDI.DIB_RGB_COLORS); final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.setRGB(0, 0, width, height, buffer.getIntArray(0, width * height), 0, width); GDI32.INSTANCE.DeleteObject(hBitmap); User32.INSTANCE.ReleaseDC(hWnd, hdcWindow); return image; }
public static BufferedImage getScreenshot( final Rectangle bounds ) { HDC windowDC = GDI.GetDC( USER.GetDesktopWindow() ); HBITMAP outputBitmap = GDI.CreateCompatibleBitmap( windowDC, bounds.width, bounds.height ); try { HDC blitDC = GDI.CreateCompatibleDC( windowDC ); try { HANDLE oldBitmap = GDI.SelectObject( blitDC, outputBitmap ); try { GDI.BitBlt( blitDC, 0, 0, bounds.width, bounds.height, windowDC, bounds.x, bounds.y, GDI32.SRCCOPY ); } finally { GDI.SelectObject( blitDC, oldBitmap ); } BITMAPINFO bi = new BITMAPINFO( 40 ); bi.bmiHeader.biSize = 40; boolean ok = GDI.GetDIBits( blitDC, outputBitmap, 0, bounds.height, (byte[]) null, bi, WinGDI.DIB_RGB_COLORS ); if ( ok ) { BITMAPINFOHEADER bih = bi.bmiHeader; bih.biHeight = -Math.abs( bih.biHeight ); bi.bmiHeader.biCompression = 0; return bufferedImageFromBitmap( blitDC, outputBitmap, bi ); } else return null; } finally { GDI.DeleteObject( blitDC ); } } finally { GDI.DeleteObject( outputBitmap ); } }
public static BufferedImage _captureAero(HWND hwnd, Rectangle bounds) { //RDW_FRAME | RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW | RDW_ALLCHILDREN int flags = 0x0400 | 0x0001 | 0x0004 | 0x0100 | 0x0080; HWND tagetHwnd = hwnd; HDC windowDC = GDI.GetDC(tagetHwnd); HBITMAP outputBitmap = GDI.CreateCompatibleBitmap(windowDC, bounds.width, bounds.height); try{ HDC blitDC = GDI.CreateCompatibleDC(windowDC); try{ HANDLE oldBitmap = GDI.SelectObject(blitDC, outputBitmap); USER.RedrawWindow(tagetHwnd, null, null, flags); if(USER.PrintWindow(tagetHwnd, blitDC, 1)){ GDI.SelectObject(blitDC, oldBitmap); WinGDI.BITMAPINFO bi = new WinGDI.BITMAPINFO(40); bi.bmiHeader.biSize = 40; boolean ok = GDI.GetDIBits(blitDC, outputBitmap, 0, bounds.height, (byte[]) null, bi, WinGDI.DIB_RGB_COLORS); if (ok) { WinGDI.BITMAPINFOHEADER bih = bi.bmiHeader; bih.biHeight = -Math.abs(bih.biHeight); bi.bmiHeader.biCompression = 0; return bufferedImageFromBitmap(blitDC, outputBitmap, bi); } else { return null; } } } finally{ GDI.DeleteObject(blitDC); } } finally { GDI.DeleteObject(outputBitmap); GDI.DeleteObject(windowDC); } return null; }
public static BufferedImage _capture(HWND hwnd, Rectangle bounds) { HDC windowDC = null; if(hwnd == null){ windowDC = GDI.GetDC(USER.GetDesktopWindow()); } else { windowDC = GDI.GetDC(hwnd); } HBITMAP outputBitmap = GDI.CreateCompatibleBitmap(windowDC, bounds.width, bounds.height); try { WinDef.HDC blitDC = GDI.CreateCompatibleDC(windowDC); try { WinNT.HANDLE oldBitmap = GDI.SelectObject(blitDC, outputBitmap); try { GDI.BitBlt(blitDC, 0, 0, bounds.width, bounds.height, windowDC, bounds.x, bounds.y, GDI32.SRCCOPY); } finally { GDI.SelectObject(blitDC, oldBitmap); } WinGDI.BITMAPINFO bi = new WinGDI.BITMAPINFO(40); bi.bmiHeader.biSize = 40; boolean ok = GDI.GetDIBits(blitDC, outputBitmap, 0, bounds.height, (byte[]) null, bi, WinGDI.DIB_RGB_COLORS); if (ok) { WinGDI.BITMAPINFOHEADER bih = bi.bmiHeader; bih.biHeight = -Math.abs(bih.biHeight); bi.bmiHeader.biCompression = 0; return bufferedImageFromBitmap(blitDC, outputBitmap, bi); } else { return null; } } finally { GDI.DeleteObject(blitDC); } } finally { GDI.DeleteObject(outputBitmap); GDI.DeleteObject(windowDC); } }
boolean GetDIBits(HDC dc, HBITMAP bmp, int startScan, int scanLines, byte[] pixels, BITMAPINFO bi, int usage);
boolean GetDIBits(HDC dc, HBITMAP bmp, int startScan, int scanLines, short[] pixels, BITMAPINFO bi, int usage);
boolean GetDIBits(HDC dc, HBITMAP bmp, int startScan, int scanLines, int[] pixels, BITMAPINFO bi, int usage);
private static BufferedImage capture(HWND hWnd) throws WindowNotFoundException { HDC hdcWindow = User32.INSTANCE.GetDC(hWnd); HDC hdcMemDC = GDI32.INSTANCE.CreateCompatibleDC(hdcWindow); RECT bounds = new RECT(); User32Extra.INSTANCE.GetClientRect(hWnd, bounds); int width = bounds.right - bounds.left; int height = bounds.bottom - bounds.top; if(width == 0 || height == 0) throw new peeknick.errormanager.WindowNotFoundException(); HBITMAP hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcWindow, width, height); HANDLE hOld = GDI32.INSTANCE.SelectObject(hdcMemDC, hBitmap); GDI32Extra.INSTANCE.BitBlt(hdcMemDC, 0, 0, width, height, hdcWindow, 0, 0, WinGDIExtra.SRCCOPY); GDI32.INSTANCE.SelectObject(hdcMemDC, hOld); GDI32.INSTANCE.DeleteDC(hdcMemDC); BITMAPINFO bmi = new BITMAPINFO(); bmi.bmiHeader.biWidth = width; bmi.bmiHeader.biHeight = -height; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = WinGDI.BI_RGB; Memory buffer = new Memory(width * height * 4); GDI32.INSTANCE.GetDIBits(hdcWindow, hBitmap, 0, height, buffer, bmi, WinGDI.DIB_RGB_COLORS); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.setRGB(0, 0, width, height, buffer.getIntArray(0, width * height), 0, width); GDI32.INSTANCE.DeleteObject(hBitmap); User32.INSTANCE.ReleaseDC(hWnd, hdcWindow); return image; }
/** * The CreateDIBitmap function creates a compatible bitmap (DDB) from a DIB * and, optionally, sets the bitmap bits. * * @param hDC Handle to a device context. * @param lpbmih Pointer to a bitmap information header structure, which may * be one of those shown in the following table. * @param fdwInit Specifies how the system initializes the bitmap bits. * @param lpbInit Pointer to an array of bytes containing the initial bitmap * data. * @param lpbmi Pointer to a BITMAPINFO structure that describes the * dimensions and color format of the array pointed to by the lpbInit * parameter. * @param fuUsage Specifies whether the bmiColors member of the BITMAPINFO * structure was initialized and, if so, whether bmiColors contains explicit * red, green, blue (RGB) values or palette indexes. The fuUsage parameter * must be one of the following values. * @return If the function succeeds, the return value is a handle to the * compatible bitmap. If the function fails, the return value is NULL. To * get extended error information, call GetLastError. */ HBITMAP CreateDIBitmap(HDC hDC, BITMAPINFOHEADER lpbmih, int fdwInit, Pointer lpbInit, BITMAPINFO lpbmi, int fuUsage);
/** * The CreateDIBSection function creates a DIB that applications can write * to directly. The function gives you a pointer to the location of the * bitmap bit values. You can supply a handle to a file-mapping object that * the function will use to create the bitmap, or you can let the system * allocate the memory for the bitmap. * * @param hDC Handle to a device context. If the value of iUsage is * DIB_PAL_COLORS, the function uses this device context's logical palette * to initialize the DIB colors. * @param pbmi Pointer to a BITMAPINFO structure that specifies various * attributes of the DIB, including the bitmap dimensions and colors. * @param iUsage Specifies the type of data contained in the bmiColors array * member of the BITMAPINFO structure pointed to by pbmi (either logical * palette indexes or literal RGB values). * @param ppvBits Pointer to a variable that receives a pointer to the * location of the DIB bit values. * @param hSection Handle to a file-mapping object that the function will * use to create the DIB. This parameter can be NULL. * @param dwOffset Specifies the offset from the beginning of the * file-mapping object referenced by hSection where storage for the bitmap * bit values is to begin. * @return Specifies the offset from the beginning of the file-mapping * object referenced by hSection where storage for the bitmap bit values is * to begin. */ HBITMAP CreateDIBSection(HDC hDC, BITMAPINFO pbmi, int iUsage, PointerByReference ppvBits, Pointer hSection, int dwOffset);
/** * The CreateCompatibleBitmap function creates a bitmap compatible with the * device that is associated with the specified device context. * * @param hDC Handle to a device context. * @param width Specifies the bitmap width, in pixels. * @param height Specifies the bitmap height, in pixels. * @return If the function succeeds, the return value is a handle to the * compatible bitmap (DDB). If the function fails, the return value is NULL. * To get extended error information, call GetLastError. */ HBITMAP CreateCompatibleBitmap(HDC hDC, int width, int height);
/** * The GetDIBits function retrieves the bits fo the specified compatible * bitmap and copies them into a buffer as a DIB using the specified format. * * @param hdc A handle to the device context. * @param hbmp A handle to the bitmap. This must be a compatible bitmap * (DDB). * @param uStartScan The first scan line to retrieve * @param cScanLines The number of scan lines to retrieve. * @param lpvBits A pointer to a buffer to receive the bitmap data. If this * parameter is <code>null</code>, the function passes the dimensions and * format of the bitmap to the {@link BITMAPINFO} structure pointed to by * the <i>lpbi</i> parameter. * @param lpbi A pointer to a {@link BITMAPINFO} structure that specifies * the desired format for the DIB data. * @param uUsage The format of the bmiColors member of the {@link * BITMAPINFO} structure. * @return the number of scan lines copied from the bitmap */ int GetDIBits(HDC hdc, HBITMAP hbmp, int uStartScan, int cScanLines, Pointer lpvBits, BITMAPINFO lpbi, int uUsage);
/** * Retrieves the dimension of the bitmap passed into it. * * @param hBitmap [in] A handle to a compatible bitmap (DDB). * @param lpDimension [out]* A pointer to a SIZE structure to receive the bitmap dimensions. For more information, see Remarks. * @return <b>true</b> if the function succeeds, <b>false</b> if the function fails. * Remarks * The function returns a data structure that contains fields for the height and width of the bitmap, in .01-mm units. If those dimensions have not yet been set, the structure that is returned will have zeroes in those fields */ public boolean GetBitmapDimensionEx(HBITMAP hBitmap, SIZE size);
/** The GetDIBits function retrieves the bits fo the specified compatible * bitmap and copies them into a buffer as a DIB using the specified * format. * @param hdc A handle to the device context. * @param hbmp A handle to the bitmap. This must be a compatible bitmap * (DDB). * @param uStartScan The first scan line to retrieve * @param cScanLines The number of scan lines to retrieve. * @param lpvBits A pointer to a buffer to receive the bitmap data. If * this parameter is <code>null</code>, the function passes the dimensions * and format of the bitmap to the {@link BITMAPINFOHEADER_MANUAL} structure pointed to * by the <i>lpbi</i> parameter. * @param lpbi A pointer to a {@link BITMAPINFOHEADER_MANUAL} structure that specifies * the desired format for the DIB data. * @param uUsage The format of the bmiColors member of the {@link * BITMAPINFO} structure. This method will not return those as they don't exist in the BITMAPINFOHEADER * structure. */ int GetDIBits(HDC hdc, HBITMAP hbmp, int uStartScan, int cScanLines, Pointer lpvBits, BITMAPINFOHEADER_MANUAL lpbi, int uUsage);
boolean GetDIBits( HDC dc, HBITMAP bmp, int startScan, int scanLines, byte[] pixels, BITMAPINFO bi, int usage );
boolean GetDIBits( HDC dc, HBITMAP bmp, int startScan, int scanLines, short[] pixels, BITMAPINFO bi, int usage );
boolean GetDIBits( HDC dc, HBITMAP bmp, int startScan, int scanLines, int[] pixels, BITMAPINFO bi, int usage );