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);
}
});

本文介绍了使用SWT和GC进行图像缩放的两种方法:一是利用GC的drawImage方法实现图像拉伸与裁剪;二是通过ImageData的scaledTo方法创建按比例缩放的新ImageData对象。文中还提供了具体代码实例,展示了不同缩放效果的实现。
1561

被折叠的 条评论
为什么被折叠?



