从文件创建BufferedImage并使其成为TYPE_INT_ARGB [英] Create a BufferedImage from file and make it TYPE_INT_ARGB

查看:1746
本文介绍了从文件创建BufferedImage并使其成为TYPE_INT_ARGB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带透明度的PNG文件,加载并存储在 BufferedImage 中。我需要 BufferedImage TYPE_INT_ARGB 。但是,当我使用 getType()时,返回的值为0( TYPE_CUSTOM )而不是2( TYPE_INT_ARGB )。

I have a PNG file with transparency that is loaded and stored in a BufferedImage. I need this BufferedImage to be of TYPE_INT_ARGB. However, when I use getType() the returned value is 0 (TYPE_CUSTOM) instead of 2 (TYPE_INT_ARGB).

这是我加载 .png 的方法:

public File img = new File("imagen.png");

public BufferedImage buffImg = 
    new BufferedImage(240, 240, BufferedImage.TYPE_INT_ARGB);

try { 
    buffImg = ImageIO.read(img ); 
} 
catch (IOException e) { }

System.out.Println(buffImg.getType()); //Prints 0 instead of 2

如何加载.png,保存在<$ c中$ c> BufferedImage 并将其设为 TYPE_INT_ARGB

How can I load the .png, save in the BufferedImage and make it TYPE_INT_ARGB?

推荐答案

BufferedImage in = ImageIO.read(img);

BufferedImage newImage = new BufferedImage(
    in.getWidth(), in.getHeight(), BufferedImage.TYPE_INT_ARGB);

Graphics2D g = newImage.createGraphics();
g.drawImage(in, 0, 0, null);
g.dispose();

这篇关于从文件创建BufferedImage并使其成为TYPE_INT_ARGB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆