@@ -1089,47 +1089,49 @@ public Color getBackground() {
10891089
10901090 /* Get the HDC for the device */
10911091 long hDC = device .internal_new_GC (null );
1092- long handle = win32_getHandle (this , getZoom ());
1093-
1094- /* Compute the background color */
1095- BITMAP bm = new BITMAP ();
1096- OS .GetObject (handle , BITMAP .sizeof , bm );
1097- long hdcMem = OS .CreateCompatibleDC (hDC );
1098- long hOldObject = OS .SelectObject (hdcMem , handle );
1099- int red = 0 , green = 0 , blue = 0 ;
1100- if (bm .bmBitsPixel <= 8 ) {
1101- byte [] color = new byte [4 ];
1102- OS .GetDIBColorTable (hdcMem , transparentPixel , 1 , color );
1103- blue = color [0 ] & 0xFF ;
1104- green = color [1 ] & 0xFF ;
1105- red = color [2 ] & 0xFF ;
1106- } else {
1107- switch (bm .bmBitsPixel ) {
1108- case 16 :
1109- blue = (transparentPixel & 0x1F ) << 3 ;
1110- green = (transparentPixel & 0x3E0 ) >> 2 ;
1111- red = (transparentPixel & 0x7C00 ) >> 7 ;
1112- break ;
1113- case 24 :
1114- blue = (transparentPixel & 0xFF0000 ) >> 16 ;
1115- green = (transparentPixel & 0xFF00 ) >> 8 ;
1116- red = transparentPixel & 0xFF ;
1117- break ;
1118- case 32 :
1119- blue = (transparentPixel & 0xFF000000 ) >>> 24 ;
1120- green = (transparentPixel & 0xFF0000 ) >> 16 ;
1121- red = (transparentPixel & 0xFF00 ) >> 8 ;
1122- break ;
1123- default :
1124- return null ;
1092+ return applyUsingAnyHandle (imageHandle -> {
1093+ long handle = imageHandle .handle ;
1094+ /* Compute the background color */
1095+ BITMAP bm = new BITMAP ();
1096+ OS .GetObject (handle , BITMAP .sizeof , bm );
1097+ long hdcMem = OS .CreateCompatibleDC (hDC );
1098+ long hOldObject = OS .SelectObject (hdcMem , handle );
1099+ int red = 0 , green = 0 , blue = 0 ;
1100+ if (bm .bmBitsPixel <= 8 ) {
1101+ byte [] color = new byte [4 ];
1102+ OS .GetDIBColorTable (hdcMem , transparentPixel , 1 , color );
1103+ blue = color [0 ] & 0xFF ;
1104+ green = color [1 ] & 0xFF ;
1105+ red = color [2 ] & 0xFF ;
1106+ } else {
1107+ switch (bm .bmBitsPixel ) {
1108+ case 16 :
1109+ blue = (transparentPixel & 0x1F ) << 3 ;
1110+ green = (transparentPixel & 0x3E0 ) >> 2 ;
1111+ red = (transparentPixel & 0x7C00 ) >> 7 ;
1112+ break ;
1113+ case 24 :
1114+ blue = (transparentPixel & 0xFF0000 ) >> 16 ;
1115+ green = (transparentPixel & 0xFF00 ) >> 8 ;
1116+ red = transparentPixel & 0xFF ;
1117+ break ;
1118+ case 32 :
1119+ blue = (transparentPixel & 0xFF000000 ) >>> 24 ;
1120+ green = (transparentPixel & 0xFF0000 ) >> 16 ;
1121+ red = (transparentPixel & 0xFF00 ) >> 8 ;
1122+ break ;
1123+ default :
1124+ return null ;
1125+ }
11251126 }
1126- }
1127- OS .SelectObject (hdcMem , hOldObject );
1128- OS .DeleteDC (hdcMem );
1127+ OS .SelectObject (hdcMem , hOldObject );
1128+ OS .DeleteDC (hdcMem );
11291129
1130- /* Release the HDC for the device */
1131- device .internal_dispose_GC (hDC , null );
1132- return Color .win32_new (device , (blue << 16 ) | (green << 8 ) | red );
1130+
1131+ /* Release the HDC for the device */
1132+ device .internal_dispose_GC (hDC , null );
1133+ return Color .win32_new (device , (blue << 16 ) | (green << 8 ) | red );
1134+ });
11331135}
11341136
11351137/**
@@ -1176,7 +1178,7 @@ Rectangle getBounds(int zoom) {
11761178 */
11771179@ Deprecated
11781180public Rectangle getBoundsInPixels () {
1179- return getBounds ( getZoom () );
1181+ return applyUsingAnyHandle ( ImageHandle :: getBounds );
11801182}
11811183
11821184/**
@@ -1258,7 +1260,7 @@ public ImageData getImageData (int zoom) {
12581260 */
12591261@ Deprecated
12601262public ImageData getImageDataAtCurrentZoom () {
1261- return getImageMetadata ( getZoom ()). getImageData ( );
1263+ return applyUsingAnyHandle ( ImageHandle :: getImageData );
12621264}
12631265
12641266/**
@@ -1832,6 +1834,18 @@ public String toString () {
18321834 return "Image {" + zoomLevelToImageHandle + "}" ;
18331835}
18341836
1837+ <T > T applyUsingAnyHandle (Function <ImageHandle , T > function ) {
1838+ if (zoomLevelToImageHandle .isEmpty ()) {
1839+ ImageHandle temporaryHandle = this .imageProvider .newImageHandle (DPIUtil .getDeviceZoom ());
1840+ try {
1841+ return function .apply (temporaryHandle );
1842+ } finally {
1843+ temporaryHandle .destroy ();
1844+ }
1845+ }
1846+ return function .apply (zoomLevelToImageHandle .values ().iterator ().next ());
1847+ }
1848+
18351849/**
18361850 * Invokes platform specific functionality to allocate a new image.
18371851 * <p>
@@ -2558,6 +2572,10 @@ public ImageHandle(long handle, int zoom) {
25582572 setImageMetadataForHandle (this , zoom );
25592573 }
25602574
2575+ public Rectangle getBounds () {
2576+ return new Rectangle (0 , 0 , width , height );
2577+ }
2578+
25612579 private void setBackground (RGB color ) {
25622580 if (transparentPixel == -1 ) return ;
25632581
0 commit comments