Java SWT Image 图像 —— 放大 缩小 Scaling

There are times when this will not always be the case and you need to stretch or shrink the image, and there are two ways to do achieve this. The first is to use the GC to stretch and clip it, using GC.drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int dstX, int dstY, int dstWidth, int dstHeight), and the second is to use ImageData.scaledTo(int width, int height) to create a new ImageData object based on scaling the receiver.

 

1,GC.drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int dstX, int dstY, int dstWidth, int dstHeight);

2,ImageData.scaledTo(int width, int height)

 

		final Image image = new Image(display,
				getClass(),getResourceAsStream("Idea.jpg"));
		final int width = image.getBounds().width;
		final int height = image.getBounds().height;

		final Image scaled050 = new Image(display,
				image.getImageData().scaledTo((int)(width*0.5),(int)(height*0.5)));
		final Image scaled200 = new Image(display,
				image.getImageData().scaledTo((int)(width*2),(int)(height*2)));

		final Image scaledGC200 = new Image(display,(int)(width*2),(int)(height*2));
		GC gc = new GC(scaledGC200);
		gc.drawImage(image,0,0,width,height,0,0,width*2,height*2);
		gc.dispose();

		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				e.gc.drawImage(image,0,0,width,height,0,0,(int)(width*0.5),(int)(height*0.5));
				e.gc.drawImage(scaled050,100,0);
				e.gc.drawImage(scaledGC200,0,75);
				e.gc.drawImage(scaled200,225,175);
			}
		});

 

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值