use of com.sun.jna.platform.win32.WinGDI.BITMAP in project jna by java-native-access.
the class GDI32Test method testGetObject.
public void testGetObject() throws Exception {
final ICONINFO iconInfo = new ICONINFO();
final HANDLE hImage = User32.INSTANCE.LoadImage(null, new File(getClass().getResource("/res/test_icon.ico").toURI()).getAbsolutePath(), WinUser.IMAGE_ICON, 0, 0, WinUser.LR_LOADFROMFILE);
try {
// obtain test icon from classpath
if (!User32.INSTANCE.GetIconInfo(new HICON(hImage), iconInfo))
throw new Exception("Invocation of User32.GetIconInfo() failed: " + Kernel32Util.getLastErrorMessage());
iconInfo.read();
// test GetObject method
BITMAP bmp = new BITMAP();
int nWrittenBytes = GDI32.INSTANCE.GetObject(iconInfo.hbmColor, bmp.size(), bmp.getPointer());
bmp.read();
if (nWrittenBytes <= 0)
throw new Exception("Detection of bitmap information failed: " + Kernel32Util.getLastErrorMessage());
// verify that bitmap information was successfully detected
assertEquals(32, bmp.bmHeight.intValue());
assertEquals(32, bmp.bmWidth.intValue());
} finally {
if (iconInfo.hbmColor != null && iconInfo.hbmColor.getPointer() != Pointer.NULL)
GDI32.INSTANCE.DeleteObject(iconInfo.hbmColor);
if (iconInfo.hbmMask != null && iconInfo.hbmMask.getPointer() != Pointer.NULL)
GDI32.INSTANCE.DeleteObject(iconInfo.hbmMask);
}
}
use of com.sun.jna.platform.win32.WinGDI.BITMAP in project jna by java-native-access.
the class GDI32Util method getScreenshot.
/**
* Takes a screenshot of the given window
*
* @param target
* The window to target
* @return the window captured as a screenshot, or null if the BufferedImage doesn't construct properly
* @throws IllegalStateException
* if the rectangle from GetWindowRect has a width and/or height
* of 0. <br>
* if the device context acquired from the original HWND doesn't
* release properly
*/
public static BufferedImage getScreenshot(HWND target) {
RECT rect = new RECT();
if (!User32.INSTANCE.GetWindowRect(target, rect)) {
throw new Win32Exception(Native.getLastError());
}
Rectangle jRectangle = rect.toRectangle();
int windowWidth = jRectangle.width;
int windowHeight = jRectangle.height;
if (windowWidth == 0 || windowHeight == 0) {
throw new IllegalStateException("Window width and/or height were 0 even though GetWindowRect did not appear to fail.");
}
HDC hdcTarget = User32.INSTANCE.GetDC(target);
if (hdcTarget == null) {
throw new Win32Exception(Native.getLastError());
}
Win32Exception we = null;
// device context used for drawing
HDC hdcTargetMem = null;
// handle to the bitmap to be drawn to
HBITMAP hBitmap = null;
// original display surface associated with the device context
HANDLE hOriginal = null;
// final java image structure we're returning.
BufferedImage image = null;
try {
hdcTargetMem = GDI32.INSTANCE.CreateCompatibleDC(hdcTarget);
if (hdcTargetMem == null) {
throw new Win32Exception(Native.getLastError());
}
hBitmap = GDI32.INSTANCE.CreateCompatibleBitmap(hdcTarget, windowWidth, windowHeight);
if (hBitmap == null) {
throw new Win32Exception(Native.getLastError());
}
hOriginal = GDI32.INSTANCE.SelectObject(hdcTargetMem, hBitmap);
if (hOriginal == null) {
throw new Win32Exception(Native.getLastError());
}
// draw to the bitmap
if (!GDI32.INSTANCE.BitBlt(hdcTargetMem, 0, 0, windowWidth, windowHeight, hdcTarget, 0, 0, GDI32.SRCCOPY)) {
throw new Win32Exception(Native.getLastError());
}
BITMAPINFO bmi = new BITMAPINFO();
bmi.bmiHeader.biWidth = windowWidth;
bmi.bmiHeader.biHeight = -windowHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
Memory buffer = new Memory(windowWidth * windowHeight * 4);
int resultOfDrawing = GDI32.INSTANCE.GetDIBits(hdcTarget, hBitmap, 0, windowHeight, buffer, bmi, WinGDI.DIB_RGB_COLORS);
if (resultOfDrawing == 0 || resultOfDrawing == WinError.ERROR_INVALID_PARAMETER) {
throw new Win32Exception(Native.getLastError());
}
int bufferSize = windowWidth * windowHeight;
DataBuffer dataBuffer = new DataBufferInt(buffer.getIntArray(0, bufferSize), bufferSize);
WritableRaster raster = Raster.createPackedRaster(dataBuffer, windowWidth, windowHeight, windowWidth, SCREENSHOT_BAND_MASKS, null);
image = new BufferedImage(SCREENSHOT_COLOR_MODEL, raster, false, null);
} catch (Win32Exception e) {
we = e;
} finally {
if (hOriginal != null) {
// per MSDN, set the display surface back when done drawing
HANDLE result = GDI32.INSTANCE.SelectObject(hdcTargetMem, hOriginal);
// failure modes are null or equal to HGDI_ERROR
if (result == null || WinGDI.HGDI_ERROR.equals(result)) {
Win32Exception ex = new Win32Exception(Native.getLastError());
if (we != null) {
ex.addSuppressed(we);
}
we = ex;
}
}
if (hBitmap != null) {
if (!GDI32.INSTANCE.DeleteObject(hBitmap)) {
Win32Exception ex = new Win32Exception(Native.getLastError());
if (we != null) {
ex.addSuppressed(we);
}
we = ex;
}
}
if (hdcTargetMem != null) {
// get rid of the device context when done
if (!GDI32.INSTANCE.DeleteDC(hdcTargetMem)) {
Win32Exception ex = new Win32Exception(Native.getLastError());
if (we != null) {
ex.addSuppressed(we);
}
we = ex;
}
}
if (hdcTarget != null) {
if (0 == User32.INSTANCE.ReleaseDC(target, hdcTarget)) {
throw new IllegalStateException("Device context did not release properly.");
}
}
}
if (we != null) {
throw we;
}
return image;
}
Aggregations