/** * Gets the icon that corresponds to a given icon handler. * * @param hIcon * Handler to the icon to get * @return The icon that corresponds to a given icon handler */ public static BufferedImage getIcon(final HICON hIcon) { final int width = ICON_SIZE; final int height = ICON_SIZE; final short depth = ICON_DEPTH; final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); final Memory lpBitsColor = new Memory(width * height * depth / ICON_BYTE_SIZE); final Memory lpBitsMask = new Memory(width * height * depth / ICON_BYTE_SIZE); final BITMAPINFO info = new BITMAPINFO(); final BITMAPINFOHEADER hdr = new BITMAPINFOHEADER(); info.bmiHeader = hdr; hdr.biWidth = width; hdr.biHeight = height; hdr.biPlanes = 1; hdr.biBitCount = depth; hdr.biCompression = WinGDI.BI_RGB; final HDC hDC = User32.INSTANCE.GetDC(null); final ICONINFO piconinfo = new ICONINFO(); User32.INSTANCE.GetIconInfo(hIcon, piconinfo); GDI32.INSTANCE.GetDIBits(hDC, piconinfo.hbmColor, 0, height, lpBitsColor, info, WinGDI.DIB_RGB_COLORS); GDI32.INSTANCE.GetDIBits(hDC, piconinfo.hbmMask, 0, height, lpBitsMask, info, WinGDI.DIB_RGB_COLORS); int r, g, b, a, argb; int x = 0, y = height - 1; for (int i = 0; i < lpBitsColor.size(); i = i + 3) { b = lpBitsColor.getByte(i) & 0xFF; g = lpBitsColor.getByte(i + 1) & 0xFF; r = lpBitsColor.getByte(i + 2) & 0xFF; a = 0xFF - lpBitsMask.getByte(i) & 0xFF; argb = a << 24 | r << 16 | g << 8 | b; image.setRGB(x, y, argb); x = (x + 1) % width; if (x == 0) { y--; } } User32.INSTANCE.ReleaseDC(null, hDC); GDI32.INSTANCE.DeleteObject(piconinfo.hbmColor); GDI32.INSTANCE.DeleteObject(piconinfo.hbmMask); return image; }
@Override public BufferedImage getScreenshot() { final HANDLE oldBitmap = GDI.SelectObject( blitDC, outputBitmap ); try { GDI.BitBlt( blitDC, 0, 0, screenArea.width, screenArea.height, windowDC, screenArea.x, screenArea.y, GDI32.SRCCOPY ); } finally { GDI.SelectObject( blitDC, oldBitmap ); } final boolean ok = GDI.GetDIBits( blitDC, outputBitmap, 0, screenArea.height, (byte[]) null, bi, WinGDI.DIB_RGB_COLORS ); if ( ok ) { final BITMAPINFOHEADER bih = bi.bmiHeader; bih.biHeight = -Math.abs( bih.biHeight ); bih.biCompression = 0; return bufferedImageFromBitmap( blitDC, outputBitmap ); } else return null; }
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 ); } }
/** * 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);