1

I've search a lot, but yet i still can't figure out where's the issue. The image display as i expected but it doesn't save it.

I've watch answers like this:
https://stackoverflow.com/questions/12575201/how-to-save-a-image-on-jframe\ How to save an Image from a JLabel

Looks very similar the save process. I even download a simple code using a similar way and it works. i've tried with "paint()" instead of "printAll()" and don't work neither.

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class BorderImage extends JFrame {
    
    public static void main(String[] args){ 
        try {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            JFileChooser fc = new JFileChooser();
            
            if(fc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION){
                BufferedImage img = ImageIO.read(fc.getSelectedFile());
                JLabel label = new JLabel(new ImageIcon(img), JLabel.CENTER);
                
                int w = img.getWidth();
                int h = img.getHeight();
                if (w > h) {
                    int diff = (w - h)/2;
                    frame.getContentPane().setBackground(Color.BLACK);
                    frame.setSize(w, w);
                } else if (h > w) {
                    frame.getContentPane().setBackground(Color.BLACK);
                    frame.setSize(h, h);
                }       
                
                frame.getContentPane().add(label);
                frame.setVisible(true);
                
                try {
                    BufferedImage save = new BufferedImage(frame.getWidth(),
                            frame.getHeight(), BufferedImage.TYPE_INT_ARGB);    
                    Graphics2D graphics = save.createGraphics();                
                    frame.printAll(graphics);               
                    graphics.dispose(); 
                    ImageIO.write(save, "jpg", new File("newImage.jpg"));
                    
                    } catch (IOException x) {
                        x.printStackTrace();
                        }
                }  
            } catch(IOException e) {
                e.printStackTrace();
            }
        }  
    }
Ginazai
  • 15
  • 1
  • 5
  • 1
    What exactly is going wrong? Is an exception being thrown? Can you not find where the image is being saved to? If so, have you tried using an absolute path like `"C:/newImage.jpg"`? – Jonny Henly Oct 07 '20 at 20:55
  • that's the biggest problem. Any exception is thrown, everything seems right, and yes, i've tried an absolute path. – Ginazai Oct 07 '20 at 21:18
  • 1
    @Ginazai (1+) Good question and [mre]. I've never noticed this behaviour before so I also learned somthing. – camickr Oct 07 '20 at 21:38

1 Answers1

3
BufferedImage save = new BufferedImage(..., ..., BufferedImage.TYPE_INT_ARGB); 

It appears that jpg files don't like the image type ARGB.

If you want the alpha then png works (for me).

It you want jpg then you can use an image type of RGB:

BufferedImage save = new BufferedImage(..., ..., BufferedImage.TYPE_INT_RGB); 
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    It works. Thank you very, but really, very much. It may be a simple issue but since i had no clue where to search it was really frustrating. – Ginazai Oct 07 '20 at 21:34