resizing animated GIF while keeping it's animation using java

后端 未结 3 1448
暗喜
暗喜 2020-12-30 14:00

i am using Graphics2D in java to resize images, it works perfect with jpg,png and other formats. my problem is the animated GIF images, after re-sizing the animation is gone

3条回答
  •  执念已碎
    2020-12-30 14:47

    Jonny March's solution did not work for me because it processes and outputs only the first image of GIF file.

    Here is my solution, it keeps the animation while resizing.

    File f = new File("path of your animated gif");
    URL img = f.toURL();
    ImageIcon icon = new ImageIcon(img);
    //You have to convert it to URL because ImageIO just ruins the animation
    
    int width = 100;
    int height = 100;
    icon.setImage(icon.getImage().getScaledInstance(width, height,Image.SCALE_DEFAULT));
    

提交回复
热议问题