Java PNG to JPG Bug


I am trying to convert a PNG image to a JPEG image following <a href="http://www.codebeach.com/2007/06/convert-png-to-jpg-in-java.html" rel="nofollow">this</a> tutorial. But I encounter a problem. The resulting image has a pink layer.

Does anyone have a solution for this problem? Or what code should I use in order to convert the image into the desired format?

Thanks in advance!



Create a BufferedImage of desired size, e.g.:

BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB)

</li> <li>

fill it with a proper background color:


</li> <li>

Call drawImage on the image's graphics atop of that background:

img.getGraphics().drawImage(image, 0, 0, null);

</li> </ol>

then write down your image as JPG as usual.


Which color mode are you using? While you create buffered image object, try adding the type like this option.

File newFile = new File(path + fileName + "." + Strings.FILE_TYPE); Image image = null; try { image = ImageIO.read(url); // I was using an image from web } catch (IOException e1) { e1.printStackTrace(); } image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH); try { BufferedImage img = toBufferedImage(image); ImageIO.write(img, "jpg", newFile); } catch (IOException e) { e.printStackTrace(); } } private static BufferedImage toBufferedImage(Image src) { int w = src.getWidth(null); int h = src.getHeight(null); int type = BufferedImage.TYPE_INT_RGB; // other options BufferedImage dest = new BufferedImage(w, h, type); Graphics2D g2 = dest.createGraphics(); g2.drawImage(src, 0, 0, null); g2.dispose(); return dest; }


  • How to sort array of FileInfo objects in descending order without LINQ
  • insert the data in database when keyboard's done button is click in android
  • Is there any reason to have a property with no getter?
  • convert kABPersonPhoneProperty to arabic numbers
  • What do folks use app/services/ in rails applications
  • Understanding HttpServletRequest and cookies in JSF
  • Display Windows Touch Keyboard
  • Apple Push Notifications - App store submission
  • Android BottomNavigationView with Badge [closed]
  • Count of values within specified range of value in each row using data.table
  • How to change WebBrowser fullscreen video mode?
  • how to transform dataframe that contains list in every row of each column
  • Joining across databases with dbplyr
  • Raphael-GWT: fill image of a shape (Rect) appears offset. How to resolve this?
  • Threads and Concurrent Modification Exception working with a list
  • can you use embedded ruby in custom javascript files in rails?
  • $this->a->b->c->d calling methods from a superclass in php
  • Load php page results into div with ajax with link?
  • Android studio import problems. (Apktool)
  • How to display converted time zones in a 'generic week' (Sunday thru Saturday)?
  • Use AutoIt with java applications
  • Run a form (insert/update/delete) from within a div using jquery
  • How to select multiple items from a List view - JavaFX 8
  • How can i move Clearcase dyamic/snapshot views to another host (Linux)
  • Google Spreadsheet Script to Blink a range of Cells
  • Python 3x- Compression Makes File Bigger :(
  • Debug `Unexpected end of JSON input Error` on content script
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Angular 4: Responsive Grid List
  • Using Service Component Runtime
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • How to handle a codeigniter PDF generator