查找 .gif 文件的尺寸

Finding dimensions of a .gif file

我正在寻找一种方法来查找 Java 中 .gif 文件的像素尺寸。

我是否必须将其中一张图像从 gif 转换为 BufferedImage 并从那里获取它,还是有更简单的方法?

使用 ImageIO 不起作用,因为我需要使用 ImageIcons 来使用 Swing 绘制 gif。我是这样画的。

attackImage = new ImageIcon("assets/Attacks/EnergyAttack.gif").getImage();
g.drawImage(attackImage, getX() + 20, getY(), null);

我引用你的话:

I need to use ImageIcons to draw a gif using Swing.

你试过用ImageIcon吗?getIconHeight and ImageIcon.getIconWidth? Additionally, if you have a fixed-size view, you might consider using ImageIcon.getImage followed by Image.getScaledInstance.

因为您已经有一个 ImageIcon instance, you can simply use its getIconHeight() and getIconWidth() 方法来获取这些值:

ImageIcon attackImage = new ImageIcon("assets/Attacks/EnergyAttack.gif");
int attackImageHeight = attackImage.getIconHeight();
int attackImageWidth = attackImage.getIconWidth();
g.drawImage(attackImage.getImage(), getX() + 20, getY(), null);