简体   繁体   English

将JFrame另存为具有透明背景的图像

[英]Save JFrame as image with transparent background

I'm sorry I know there are a lot of questions like that but I just couldn't find the solution. 对不起,我知道有很多这样的问题,但是我找不到解决方案。 I tried different things but just I can't get the background transparent. 我尝试了不同的方法,但是我无法使背景透明。 Here is my test code: 这是我的测试代码:

public class Pngs extends JPanel {

public void paint(Graphics g) {
    Image img = createImage();
    g.drawImage(img, 0, 0, this);
}

public static BufferedImage getScreenShot(
        Component component) {

    BufferedImage image = new BufferedImage(
            component.getWidth(),
            component.getHeight(),
            BufferedImage.TYPE_INT_RGB
    );
// call the Component's paint method, using
    // the Graphics object of the image.
    component.paint(image.getGraphics());
    return image;
}

private static Image createImage() {
    BufferedImage img = null;
    try { 
        img = ImageIO.read(new File("oneImg.png"));
    } catch (IOException e) {
    }
    return img;
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new Pngs());
    frame.setUndecorated(true);
    frame.getContentPane().setBackground(new Color(1.0f, 1.0f, 1.0f, 0.5f));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(512, 512);
    frame.setVisible(true);
    try { //saves the image
        // retrieve image
        BufferedImage bi = getScreenShot(frame.getContentPane());
        File outputfile = new File("saved.png");
        ImageIO.write(bi, "png", outputfile);
    } catch (IOException e) {
    }
}

}

I tried to set the 4th argument of the Color constructor to 0 so it will be fully transparent but then I just get a black background. 我试图将Color构造函数的第4个参数设置为0,因此它将是完全透明的,但随后我只得到了黑色背景。 Also when it is set to 0.5f it's not transparent at all. 同样,当设置为0.5f时,它根本不是透明的。

What could be the problem? 可能是什么问题呢?

Use BufferedImage.TYPE_INT_ARGB instead of BufferedImage.TYPE_INT_RGB . 使用BufferedImage.TYPE_INT_ARGB而不是BufferedImage.TYPE_INT_RGB
By the way: Use this code to create proper screenshots: 顺便说一句:使用此代码创建正确的屏幕截图:

public static BufferedImage getScreenShot(Component component) throws AWTException {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    Robot robot = new Robot(gd);
    Rectangle bounds = new Rectangle(component.getLocationOnScreen(), component.getSize());
    return robot.createScreenCapture(bounds);
} 

This work for me: Try This: 为我工作:尝试以下操作:

public Pngs(){  // constructor
 setBackground(new Color(0,0,0,0));
 setUndecorated(true);
 setOpacity(0.5F);

}

This make your Jframe Transparent and close option invisible. 这使您的Jframe透明和关闭选项不可见。 So, add an exit button to close your frame. 因此,添加退出按钮以关闭框架。 For more information about opacity and make JFrame transparent see THIS 有关不透明度和使JFrame透明的更多信息,请参见THIS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM