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