Skip to main content

converting ImageIcon to BufferedImage

4 replies [Last post]
vlie84
Offline
Joined: 2003-12-02

Hey guys can you tell me a way to convert ImageIcon to BufferedImage.

at the moment I'm using

<br />
 public BufferedImage iconToImage(Icon icon, Component targetComponent)<br />
    {<br />
	int w = icon.getIconWidth();<br />
	int h = icon.getIconHeight();<br />
	GraphicsConfiguration gc = targetComponent.getGraphicsConfiguration();<br />
	BufferedImage image = gc.createCompatibleImage(w,h);<br />
	Graphics2D g = image.createGraphics();<br />
	icon.paintIcon(targetComponent,g,0,0);<br />
	g.dispose();<br />
	return image;<br />
    }</p>
<p>    public BufferedImage savingImage (ImageIcon img)<br />
    {<br />
	JFrame f = new JFrame();<br />
	f.setVisible(false);<br />
	BufferedImage image = iconToImage(img,f);<br />
	f.dispose();<br />
	return image;<br />
    }<br />

this method works but take a really long time. I tried to use
<br />
public BufferedImage savingImage (ImageIcon icon)<br />
{<br />
  Image img = icon.getImage();<br />
  BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);<br />
  Graphics2D big = image.createGraphics();<br />
  Applet f = new Applet();<br />
  big.drawImage(img,0,0,f);<br />
  return image;<br />
}<br />

but it only give me null, it doesnt give me anything, if i saved the BufferedImage return from the above method i will only get whole complete blank black image.

Do you guys have any suggestion? what is the way to get from ImageIcon to a BufferedImage that is fast but work?

Thank you so much for your suggestion.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
shan-man
Offline
Joined: 2006-02-17

Hi vlie84,

vlie84 wrote:
> Hey guys can you tell me a way to convert ImageIcon
> to BufferedImage.

Assuming that the source of the ImageIcon is not animated (ie. an animated gif), you can use the following:
[code]
Image source = icon.getImage();
int w = source.getWidth(null);
int h = source.getHeight(null);
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)image.getGraphics();
g2d.drawImage(source, 0, 0, null);
g2d.dispose();
[/code]
Since ImageIcon ensures that its image is fully loaded, you can pass [i]null[/i] to [i]getWidth[/i], [i]getHeight[/i], and [i]drawImage[/i] instead of an [i]ImageObserver[/i].

If you know that your source image contains no translucent areas, then you can use [i]BufferedImage.TYPE_INT_RGB[/i] instead of [i]_ARGB[/i].

Regards,
Shannon

sbozian
Offline
Joined: 2010-08-30

Hello,

I came across this pretty old forum, as I'm looking into a very similar issue.

Your code worked for me to convert an Icon (not ImageIcon i.e. getImage() method not available) to a BufferedImage with the following changes:

<br />
int w = icon.getIconWidth();<br />
int h = icon.getIconHeight();<br />
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);<br />
Graphics2D g2d = (Graphics2D)image.getGraphics();<br />
icon.paintIcon(null, g2d, 0, 0);<br />
g2d.dispose();<br />

My only problem is the following: although using BufferedImage.TYPE_INT_ARGB solved the problem of transparency (with .png images for example), I still noticed some change in the image quality, for instance bright gray color is being modified to darker gray.

Let me mention that I am comparing between:
1. directly painting the icon on the screen
2. painting the obtained image on the screen

Are there other conversion parameters that I should take care of to avoid any change in the colors, etc.?

Any ideas?

Thanks a lot,
Shant

sbozian
Offline
Joined: 2010-08-30

Problem solved. I was painting the image at the same location several times mistakenly, which appeared visually as a change in some of the transparency colors.

Thanks,
Shant

vlie84
Offline
Joined: 2003-12-02

hey

sorry but what about Icon, how to change Icon to either ImageIcon or Icon to BufferedImage?