Talon06 Talon06 - 5 months ago 18
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(path.contains(".jpg") || path.contains(".png") || path.contains(".gif") ){
Image image = (Image) SWTResourceManager
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);