@@ -117,6 +117,12 @@ public final class Image extends Resource implements Drawable {
117117 */
118118 private int styleFlag = SWT .IMAGE_COPY ;
119119
120+ /**
121+ * Sets the color to which to map the transparent pixel.
122+ * For further info see {@link #setBackground(Color)}
123+ */
124+ private RGB backgroundColor ;
125+
120126 /**
121127 * Attribute to cache current native zoom level
122128 */
@@ -1087,6 +1093,10 @@ public boolean equals (Object object) {
10871093public Color getBackground () {
10881094 if (isDisposed ()) SWT .error (SWT .ERROR_GRAPHIC_DISPOSED );
10891095 if (transparentPixel == -1 ) return null ;
1096+ if (backgroundColor != null ) {
1097+ // if a background color was set explicitly, we use the cached color directly
1098+ return Color .win32_new (device , (backgroundColor .blue << 16 ) | (backgroundColor .green << 8 ) | backgroundColor .red );
1099+ }
10901100
10911101 /* Get the HDC for the device */
10921102 long hDC = device .internal_new_GC (null );
@@ -1838,33 +1848,10 @@ public void setBackground(Color color) {
18381848 if (isDisposed ()) SWT .error (SWT .ERROR_GRAPHIC_DISPOSED );
18391849 if (color == null ) SWT .error (SWT .ERROR_NULL_ARGUMENT );
18401850 if (color .isDisposed ()) SWT .error (SWT .ERROR_INVALID_ARGUMENT );
1841- zoomLevelToImageHandle .values ().forEach (imageHandle -> setBackground (color , imageHandle .handle ));
1842- }
1843-
1844- private void setBackground (Color color , long handle ) {
18451851 if (transparentPixel == -1 ) return ;
18461852 transparentColor = -1 ;
1847-
1848- /* Get the HDC for the device */
1849- long hDC = device .internal_new_GC (null );
1850-
1851- /* Change the background color in the image */
1852- BITMAP bm = new BITMAP ();
1853- OS .GetObject (handle , BITMAP .sizeof , bm );
1854- long hdcMem = OS .CreateCompatibleDC (hDC );
1855- OS .SelectObject (hdcMem , handle );
1856- int maxColors = 1 << bm .bmBitsPixel ;
1857- byte [] colors = new byte [maxColors * 4 ];
1858- int numColors = OS .GetDIBColorTable (hdcMem , 0 , maxColors , colors );
1859- int offset = transparentPixel * 4 ;
1860- colors [offset ] = (byte )color .getBlue ();
1861- colors [offset + 1 ] = (byte )color .getGreen ();
1862- colors [offset + 2 ] = (byte )color .getRed ();
1863- OS .SetDIBColorTable (hdcMem , 0 , numColors , colors );
1864- OS .DeleteDC (hdcMem );
1865-
1866- /* Release the HDC for the device */
1867- device .internal_dispose_GC (hDC , null );
1853+ backgroundColor = color .getRGB ();
1854+ zoomLevelToImageHandle .values ().forEach (imageHandle -> imageHandle .setBackground (backgroundColor ));
18681855}
18691856
18701857private int getZoom () {
@@ -2403,9 +2390,37 @@ public ImageHandle(long handle, int zoom) {
24032390 this .handle = handle ;
24042391 this .zoom = zoom ;
24052392 updateBoundsInPixelsFromNative ();
2393+ if (backgroundColor != null ) {
2394+ setBackground (backgroundColor );
2395+ }
24062396 setImageMetadataForHandle (this , zoom );
24072397 }
24082398
2399+ private void setBackground (RGB color ) {
2400+ if (transparentPixel == -1 ) return ;
2401+
2402+ /* Get the HDC for the device */
2403+ long hDC = device .internal_new_GC (null );
2404+
2405+ /* Change the background color in the image */
2406+ BITMAP bm = new BITMAP ();
2407+ OS .GetObject (handle , BITMAP .sizeof , bm );
2408+ long hdcMem = OS .CreateCompatibleDC (hDC );
2409+ OS .SelectObject (hdcMem , handle );
2410+ int maxColors = 1 << bm .bmBitsPixel ;
2411+ byte [] colors = new byte [maxColors * 4 ];
2412+ int numColors = OS .GetDIBColorTable (hdcMem , 0 , maxColors , colors );
2413+ int offset = transparentPixel * 4 ;
2414+ colors [offset ] = (byte )color .blue ;
2415+ colors [offset + 1 ] = (byte )color .green ;
2416+ colors [offset + 2 ] = (byte )color .red ;
2417+ OS .SetDIBColorTable (hdcMem , 0 , numColors , colors );
2418+ OS .DeleteDC (hdcMem );
2419+
2420+ /* Release the HDC for the device */
2421+ device .internal_dispose_GC (hDC , null );
2422+ }
2423+
24092424 private void updateBoundsInPixelsFromNative () {
24102425 switch (type ) {
24112426 case SWT .BITMAP :
0 commit comments