@@ -491,13 +491,38 @@ public void copyArea (Image image, int x, int y) {
491491	storeAndApplyOperationForExistingHandle (new  CopyAreaToImageOperation (image , x , y ));
492492}
493493
494- private  class  CopyAreaToImageOperation  extends  Operation  {
495- 	private  final  Image  image ;
494+ private  abstract  class  ImageOperation  extends  Operation  {
495+ 	private  Image  image ;
496+ 
497+ 	ImageOperation (Image  image ) {
498+ 		setImage (image );
499+ 		image .addOnDisposeListener (this ::setCopyOfImage );
500+ 	}
501+ 
502+ 	private  void  setImage (Image  image ) {
503+ 		this .image  = image ;
504+ 	}
505+ 
506+ 	private  void  setCopyOfImage (Image  image ) {
507+ 		if  (!GC .this .isDisposed ()) {
508+ 			Image  copiedImage  = new  Image (image .device , image , SWT .IMAGE_COPY );
509+ 			setImage (copiedImage );
510+ 			registerForDisposal (copiedImage );
511+ 		}
512+ 	}
513+ 
514+ 	protected  Image  getImage () {
515+ 		return  image ;
516+ 	}
517+ 
518+ }
519+ 
520+ private  class  CopyAreaToImageOperation  extends  ImageOperation  {
496521	private  final  int  x ;
497522	private  final  int  y ;
498523
499524	CopyAreaToImageOperation (Image  image , int  x , int  y ) {
500- 		this . image  =  image ;
525+ 		super ( image ) ;
501526		this .x  = x ;
502527		this .y  = y ;
503528	}
@@ -507,7 +532,7 @@ void apply() {
507532		int  zoom  = getZoom ();
508533		int  scaledX  = Win32DPIUtils .pointToPixel (drawable , this .x , zoom );
509534		int  scaledY  = Win32DPIUtils .pointToPixel (drawable , this .y , zoom );
510- 		copyAreaInPixels (this . image , scaledX , scaledY );
535+ 		copyAreaInPixels (getImage () , scaledX , scaledY );
511536	}
512537}
513538
@@ -1013,18 +1038,17 @@ public void drawImage (Image image, int x, int y) {
10131038	storeAndApplyOperationForExistingHandle (new  DrawImageOperation (image , new  Point (x , y )));
10141039}
10151040
1016- private  class  DrawImageOperation  extends  Operation  {
1017- 	private  final  Image  image ;
1041+ private  class  DrawImageOperation  extends  ImageOperation  {
10181042	private  final  Point  location ;
10191043
10201044	DrawImageOperation (Image  image , Point  location ) {
1021- 		this . image  =  image ;
1045+ 		super ( image ) ;
10221046		this .location  = location ;
10231047	}
10241048
10251049	@ Override 
10261050	void  apply () {
1027- 		drawImageInPixels (this . image , Win32DPIUtils .pointToPixel (drawable , this .location , getZoom ()));
1051+ 		drawImageInPixels (getImage () , Win32DPIUtils .pointToPixel (drawable , this .location , getZoom ()));
10281052	}
10291053
10301054	private  void  drawImageInPixels (Image  image , Point  location ) {
@@ -1081,13 +1105,12 @@ void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight,
10811105	storeAndApplyOperationForExistingHandle (new  DrawImageToImageOperation (srcImage , new  Rectangle (srcX , srcY , srcWidth , srcHeight ), new  Rectangle (destX , destY , destWidth , destHeight ), simple ));
10821106}
10831107
1084- private  class  DrawScalingImageToImageOperation  extends  Operation  {
1085- 	private  final  Image  image ;
1108+ private  class  DrawScalingImageToImageOperation  extends  ImageOperation  {
10861109	private  final  Rectangle  source ;
10871110	private  final  Rectangle  destination ;
10881111
10891112	DrawScalingImageToImageOperation (Image  image , Rectangle  source , Rectangle  destination ) {
1090- 		this . image  =  image ;
1113+ 		super ( image ) ;
10911114		this .source  = source ;
10921115		this .destination  = destination ;
10931116	}
@@ -1096,7 +1119,7 @@ private class DrawScalingImageToImageOperation extends Operation {
10961119	void  apply () {
10971120		int  gcZoom  = getZoom ();
10981121		int  srcImageZoom  = calculateZoomForImage (gcZoom , source .width , source .height , destination .width , destination .height );
1099- 		drawImage (image , source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , gcZoom , srcImageZoom );
1122+ 		drawImage (getImage () , source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , gcZoom , srcImageZoom );
11001123	}
11011124
11021125	private  Collection <Integer > getAllCurrentMonitorZooms () {
@@ -1154,22 +1177,21 @@ private void drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHei
11541177	drawImage (image , src .x , src .y , src .width , src .height , dest .x , dest .y , dest .width , dest .height , false , scaledImageZoom );
11551178}
11561179
1157- private  class  DrawImageToImageOperation  extends  Operation  {
1158- 	private  final  Image  image ;
1180+ private  class  DrawImageToImageOperation  extends  ImageOperation  {
11591181	private  final  Rectangle  source ;
11601182	private  final  Rectangle  destination ;
11611183	private  final  boolean  simple ;
11621184
11631185	DrawImageToImageOperation (Image  image , Rectangle  source , Rectangle  destination , boolean  simple ) {
1164- 		this . image  =  image ;
1186+ 		super ( image ) ;
11651187		this .source  = source ;
11661188		this .destination  = destination ;
11671189		this .simple  = simple ;
11681190	}
11691191
11701192	@ Override 
11711193	void  apply () {
1172- 		drawImage (image , source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , simple , getZoom ());
1194+ 		drawImage (getImage () , source .x , source .y , source .width , source .height , destination .x , destination .y , destination .width , destination .height , simple , getZoom ());
11731195	}
11741196}
11751197
0 commit comments