@@ -1099,47 +1099,49 @@ public Color getBackground() {
10991099
11001100 /* Get the HDC for the device */
11011101 long hDC = device .internal_new_GC (null );
1102- long handle = win32_getHandle (this , getZoom ());
1103-
1104- /* Compute the background color */
1105- BITMAP bm = new BITMAP ();
1106- OS .GetObject (handle , BITMAP .sizeof , bm );
1107- long hdcMem = OS .CreateCompatibleDC (hDC );
1108- long hOldObject = OS .SelectObject (hdcMem , handle );
1109- int red = 0 , green = 0 , blue = 0 ;
1110- if (bm .bmBitsPixel <= 8 ) {
1111- byte [] color = new byte [4 ];
1112- OS .GetDIBColorTable (hdcMem , transparentPixel , 1 , color );
1113- blue = color [0 ] & 0xFF ;
1114- green = color [1 ] & 0xFF ;
1115- red = color [2 ] & 0xFF ;
1116- } else {
1117- switch (bm .bmBitsPixel ) {
1118- case 16 :
1119- blue = (transparentPixel & 0x1F ) << 3 ;
1120- green = (transparentPixel & 0x3E0 ) >> 2 ;
1121- red = (transparentPixel & 0x7C00 ) >> 7 ;
1122- break ;
1123- case 24 :
1124- blue = (transparentPixel & 0xFF0000 ) >> 16 ;
1125- green = (transparentPixel & 0xFF00 ) >> 8 ;
1126- red = transparentPixel & 0xFF ;
1127- break ;
1128- case 32 :
1129- blue = (transparentPixel & 0xFF000000 ) >>> 24 ;
1130- green = (transparentPixel & 0xFF0000 ) >> 16 ;
1131- red = (transparentPixel & 0xFF00 ) >> 8 ;
1132- break ;
1133- default :
1134- return null ;
1102+ return applyUsingAnyHandle (imageHandle -> {
1103+ long handle = imageHandle .handle ;
1104+ /* Compute the background color */
1105+ BITMAP bm = new BITMAP ();
1106+ OS .GetObject (handle , BITMAP .sizeof , bm );
1107+ long hdcMem = OS .CreateCompatibleDC (hDC );
1108+ long hOldObject = OS .SelectObject (hdcMem , handle );
1109+ int red = 0 , green = 0 , blue = 0 ;
1110+ if (bm .bmBitsPixel <= 8 ) {
1111+ byte [] color = new byte [4 ];
1112+ OS .GetDIBColorTable (hdcMem , transparentPixel , 1 , color );
1113+ blue = color [0 ] & 0xFF ;
1114+ green = color [1 ] & 0xFF ;
1115+ red = color [2 ] & 0xFF ;
1116+ } else {
1117+ switch (bm .bmBitsPixel ) {
1118+ case 16 :
1119+ blue = (transparentPixel & 0x1F ) << 3 ;
1120+ green = (transparentPixel & 0x3E0 ) >> 2 ;
1121+ red = (transparentPixel & 0x7C00 ) >> 7 ;
1122+ break ;
1123+ case 24 :
1124+ blue = (transparentPixel & 0xFF0000 ) >> 16 ;
1125+ green = (transparentPixel & 0xFF00 ) >> 8 ;
1126+ red = transparentPixel & 0xFF ;
1127+ break ;
1128+ case 32 :
1129+ blue = (transparentPixel & 0xFF000000 ) >>> 24 ;
1130+ green = (transparentPixel & 0xFF0000 ) >> 16 ;
1131+ red = (transparentPixel & 0xFF00 ) >> 8 ;
1132+ break ;
1133+ default :
1134+ return null ;
1135+ }
11351136 }
1136- }
1137- OS .SelectObject (hdcMem , hOldObject );
1138- OS .DeleteDC (hdcMem );
1137+ OS .SelectObject (hdcMem , hOldObject );
1138+ OS .DeleteDC (hdcMem );
11391139
1140- /* Release the HDC for the device */
1141- device .internal_dispose_GC (hDC , null );
1142- return Color .win32_new (device , (blue << 16 ) | (green << 8 ) | red );
1140+
1141+ /* Release the HDC for the device */
1142+ device .internal_dispose_GC (hDC , null );
1143+ return Color .win32_new (device , (blue << 16 ) | (green << 8 ) | red );
1144+ });
11431145}
11441146
11451147/**
@@ -1186,7 +1188,7 @@ Rectangle getBounds(int zoom) {
11861188 */
11871189@ Deprecated
11881190public Rectangle getBoundsInPixels () {
1189- return getBounds ( getZoom () );
1191+ return applyUsingAnyHandle ( ImageHandle :: getBounds );
11901192}
11911193
11921194/**
@@ -1268,7 +1270,7 @@ public ImageData getImageData (int zoom) {
12681270 */
12691271@ Deprecated
12701272public ImageData getImageDataAtCurrentZoom () {
1271- return getImageMetadata ( getZoom ()). getImageData ( );
1273+ return applyUsingAnyHandle ( ImageHandle :: getImageData );
12721274}
12731275
12741276/**
@@ -1842,6 +1844,18 @@ public String toString () {
18421844 return "Image {" + zoomLevelToImageHandle + "}" ;
18431845}
18441846
1847+ <T > T applyUsingAnyHandle (Function <ImageHandle , T > function ) {
1848+ if (zoomLevelToImageHandle .isEmpty ()) {
1849+ ImageHandle temporaryHandle = this .imageProvider .newImageHandle (DPIUtil .getDeviceZoom ());
1850+ try {
1851+ return function .apply (temporaryHandle );
1852+ } finally {
1853+ temporaryHandle .destroy ();
1854+ }
1855+ }
1856+ return function .apply (zoomLevelToImageHandle .values ().iterator ().next ());
1857+ }
1858+
18451859/**
18461860 * Invokes platform specific functionality to allocate a new image.
18471861 * <p>
0 commit comments