@@ -1099,47 +1099,49 @@ public Color getBackground() {
1099
1099
1100
1100
/* Get the HDC for the device */
1101
1101
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
+ }
1135
1136
}
1136
- }
1137
- OS .SelectObject (hdcMem , hOldObject );
1138
- OS .DeleteDC (hdcMem );
1137
+ OS .SelectObject (hdcMem , hOldObject );
1138
+ OS .DeleteDC (hdcMem );
1139
1139
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
+ });
1143
1145
}
1144
1146
1145
1147
/**
@@ -1186,7 +1188,7 @@ Rectangle getBounds(int zoom) {
1186
1188
*/
1187
1189
@ Deprecated
1188
1190
public Rectangle getBoundsInPixels () {
1189
- return getBounds ( getZoom () );
1191
+ return applyUsingAnyHandle ( ImageHandle :: getBounds );
1190
1192
}
1191
1193
1192
1194
/**
@@ -1268,7 +1270,7 @@ public ImageData getImageData (int zoom) {
1268
1270
*/
1269
1271
@ Deprecated
1270
1272
public ImageData getImageDataAtCurrentZoom () {
1271
- return getImageMetadata ( getZoom ()). getImageData ( );
1273
+ return applyUsingAnyHandle ( ImageHandle :: getImageData );
1272
1274
}
1273
1275
1274
1276
/**
@@ -1842,6 +1844,18 @@ public String toString () {
1842
1844
return "Image {" + zoomLevelToImageHandle + "}" ;
1843
1845
}
1844
1846
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
+
1845
1859
/**
1846
1860
* Invokes platform specific functionality to allocate a new image.
1847
1861
* <p>
0 commit comments