@@ -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,9 @@ Rectangle getBounds(int zoom) {
11761178 */
11771179@ Deprecated
11781180public Rectangle getBoundsInPixels () {
1179- return getBounds (getZoom ());
1181+ return applyUsingAnyHandle (imageHandle -> {
1182+ return imageHandle .getBounds ();
1183+ });
11801184}
11811185
11821186/**
@@ -1258,7 +1262,9 @@ public ImageData getImageData (int zoom) {
12581262 */
12591263@ Deprecated
12601264public ImageData getImageDataAtCurrentZoom () {
1261- return getImageMetadata (getZoom ()).getImageData ();
1265+ return applyUsingAnyHandle (imageHandle -> {
1266+ return imageHandle .getImageData ();
1267+ });
12621268}
12631269
12641270/**
@@ -1832,6 +1838,19 @@ public String toString () {
18321838 return "Image {" + zoomLevelToImageHandle + "}" ;
18331839}
18341840
1841+ <T > T applyUsingAnyHandle (Function <ImageHandle , T > function ) {
1842+ if (zoomLevelToImageHandle .isEmpty ()) {
1843+ ImageHandle temporaryHandle = this .imageProvider .newImageHandle (DPIUtil .getDeviceZoom ());
1844+ try {
1845+ return function .apply (temporaryHandle );
1846+ } finally {
1847+ temporaryHandle .destroy ();
1848+ }
1849+ } else {
1850+ return function .apply (zoomLevelToImageHandle .values ().iterator ().next ());
1851+ }
1852+ }
1853+
18351854/**
18361855 * Invokes platform specific functionality to allocate a new image.
18371856 * <p>
@@ -2558,6 +2577,10 @@ public ImageHandle(long handle, int zoom) {
25582577 setImageMetadataForHandle (this , zoom );
25592578 }
25602579
2580+ public Rectangle getBounds () {
2581+ return new Rectangle (0 , 0 , width , height );
2582+ }
2583+
25612584 private void setBackground (RGB color ) {
25622585 if (transparentPixel == -1 ) return ;
25632586
0 commit comments