Talon06 Talon06 - 6 months ago 21
Java Question

Java SWT Image Resize Not Working

I am trying to resize a picture and save it however the picture I am saving is not resized.

Here is the code I am trying use.

if(CC_Files.fileExists(path)){
if(path.contains(".jpg") || path.contains(".png") || path.contains(".gif") ){
Image image = (Image) SWTResourceManager
.getImage(path);
ImageData imgData = image.getImageData();
imgData.scaledTo(150, 150);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] {imgData};
imageLoader.save(Variables.getStrResources() + "\\Pics\\" + a.getHerd_id() + ".jpg",SWT.IMAGE_JPEG);
}
}

Answer Source

Your problem is that you do not read the JavaDoc where is wrriten

ImageData#scaledTo(int width, int height) - Returns a copy of the receiver which has been stretched or shrunk to the specified size.

So the solution is:

imgData = imgData.scaledTo(150, 150);

Documentation