This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Converting PNG/GIF to JPEG: alpha channel gets black background color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Converting PNG/GIF to JPEG: alpha channel gets black background color" Watch "Converting PNG/GIF to JPEG: alpha channel gets black background color" New topic
Author

Converting PNG/GIF to JPEG: alpha channel gets black background color

Jeroen De Schutter
Greenhorn

Joined: Sep 21, 2009
Posts: 4
I am afraid in don't quite understand how imaging in Java works ...
I am storing thumbnails of image files in a database. The thumbnails are always in JPEG format. When i want to store images with transparency (GIFs or PNGs) the "background color" of the alpha channel is always rendered black in the JPEG. I'd like it to become white, but I have no idea how.

I have already tried to set the background color of the Graphics2D object to white, but without visble results.

This is my code


Please note that my app runs on OC4J 10.1.2, so i'm limited to JDK 1.4
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Jeroen De Schutter wrote:When i want to store images with transparency (GIFs or PNGs) the "background color" of the alpha channel is always rendered black in the JPEG. I'd like it to become white, but I have no idea how.

I have already tried to set the background color of the Graphics2D object to white, but without visble results.


Setting the background color of the Graphics object will have no effect unless you actually do some drawing that uses the background color. For instance you could do a vG2d.clearRect(...) before the vG2d.drawImage(...).

[edit: I had originally written vG2d.fillRect(...) but that uses the foreground color.]


bitguru blog
Jeroen De Schutter
Greenhorn

Joined: Sep 21, 2009
Posts: 4
Executing the clearRect() method seems to be the solution. Thanks a lot !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting PNG/GIF to JPEG: alpha channel gets black background color
 
Similar Threads
Image background color
quickly increase the height of an image
Graphics.drawImage not using specified background color
how to renders password procatacted pdf file into image
ConvolvedOp with / without alpha premultiplication