Talon06
Talon06

Reputation: 1796

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

Upvotes: 1

Views: 6544

Answers (3)

R.Shanmukha sai
R.Shanmukha sai

Reputation: 1

Call this method from the SWT.addListener(SWT.Close, new CustomShellCloseListener()). Tha parameters required to pass are LabelImage(Do not Use Label.getImage() ,pass the direct path),label.getBounds.width ,label.getBounds.height

protected Image resize(Image imageFromSource, int width, int height) {
    if(width>0 && height>0){
        Image scaledImage = new Image(shellCCMPFMatrixBomCompare.getDisplay(), width, height);
        GC gc = new GC(scaledImage);            //Graphics Capabilities(GC instance) in SWT used to draw an Image, graphics, display
        gc.setAntialias(SWT.ON);        // Anti aliasing is used for making the low resolution image to redraw and make into a good resolution Image
        gc.setInterpolation(SWT.HIGH);      //Interpolation is based in the Graphics, it may not work properly in some systems
        gc.drawImage(imageFromSource, 0, 0, 
                imageFromSource.getBounds().width, imageFromSource.getBounds().height, 
                0, 0, width, height);       

        /*drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight)
        Copies a rectangular area from the source image into a (potentially different sized) rectangular area in the receiver.*/

        gc.dispose();
        return scaledImage;
        }
        else return imageFromSource;
}

Upvotes: 0

Chetan Bhagat
Chetan Bhagat

Reputation: 572

Java SWT Image Resize is proper Working

ImageLoader class are used to load images from, and save images to, a file or stream

imageLoader.save(result, SWT.IMAGE_COPY)

FileDialog class allow the user to navigate the file system and select or enter a file name.

Button btnOpen = new Button(parent, SWT.NONE);
btnOpen.setBounds(200, 55, 68, 23);
btnOpen.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {

          FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
          String result = dialog.open();

           if(result!=null)
           {
               Image image=SWTResourceManager.getImage(result);
               //ImageData class are device-independent descriptions of images
               ImageData imgData = image.getImageData();
               imgData=imgData.scaledTo(200, 200);

               ImageLoader imageLoader = new ImageLoader();
               imageLoader.data = new ImageData[] {imgData};
               imageLoader.save(result, SWT.IMAGE_COPY);

               System.out.println("Width: "+imgData.width+".....Height: "+imgData.height);
               lbl_image_text.setBounds(25,88,imgData.width+10,imgData.height+10);
               lbl_image_text.setImage(SWTResourceManager.getImage(result));
           }
    }
});
btnOpen.setText("open");
CLabel lbl_image_text = new CLabel(parent, SWT.Resize);

Image size set to Label Dynamically

Button btnOpen = new Button(parent, SWT.NONE);
btnOpen.setBounds(200, 55, 68, 23);
btnOpen.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {

          FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
          String result = dialog.open();

           if(result!=null)
           {

               Image image=SWTResourceManager.getImage(result);
               //get Image width and height
               lbl_image_text.setBounds(25,88,image.getBounds().width+10,image.getBounds().height+10);
               lbl_image_text.setImage(SWTResourceManager.getImage(result));
           }
    }
});
btnOpen.setText("open");
CLabel lbl_image_text = new CLabel(parent, SWT.Resize);

Upvotes: 1

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

Upvotes: 2

Related Questions