【问题标题】:Loading only part of a file into an Image仅将文件的一部分加载到图像中
【发布时间】:2011-09-13 10:04:33
【问题描述】:

[Java]我正在使用 Images / BufferedImages(我目前不知道更好的方法)将外部图片加载到画布上。

问:是否有可能只加载/显示部分图片?例如,仅使用较大的 500x300 图片文件中的 60x60 区域。 (我相信这被称为 Sprite)使用 ImageIO 加载文件。

提前致谢。

【问题讨论】:

    标签: java


    【解决方案1】:
    BufferedImage input = ImageIO.read(new File(inputFile));
    BufferedImage output = new BufferedImage(60, 60,  BufferedImage.TYPE_INT_ARGB)
    Graphics g = output .getGraphics();
    g.drawImage(input, 0, 0, null); //adjust 0, 0 to some x, y if necessary
    ImageIO.write(output, "png", new File(outputFile));
    

    【讨论】:

    • 如何将生成的 Graphics 对象“添加”到我目前在“render()”方法中使用的 Graphics2D 对象?
    • @Acidic try Graphics2D g2d = (Graphics2D)g; 根据documentation getGraphice() - This method returns a Graphics2D, but is here for backwards compatibility.
    • 我认为我的问题在于理解 - output.getGraphics() 传递的是引用,而不是值。现在一切正常,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多