Hi Doug,

First, it's best to avoid getScaledInstance(); there are better/faster
ways to do what you want (see the Java 2D FAQ [1]).  Second, in order to
preserve the transparency from your original image, you'll need to call
g2d.setComposite(AlphaComposite.Src) before copying the original into
the new image.

Allow me to suggest some code that will resolve both of the above issues
in fewer steps:

    BufferedImage in = ImageIO.read(srcFile);
    GraphicsConfiguration gc =
        in.createGraphics().getDeviceConfiguration();
    BufferedImage out =
        gc.createCompatibleImage(targetWidth, targetHeight, BITMASK);
    Graphics2D g2d = out.createGraphics();
    g2d.setComposite(AlphaComposite.Src);
    g2d.drawImage(in, 0, 0, targetWidth, targetHeight, null);
    g2d.dispose();
    ImageIO.write(out, "png", destFile);

Thanks,
Chris

[1]
http://java.sun.com/products/java-media/2D/reference/faqs/index.html#Q_How_do_I_create_a_resized_copy

[EMAIL PROTECTED] wrote:
I am attempting to write a small utility to scale 8-bit PNG files.  However, 
when I write out the scaled image the transparent field is getting mapped to a 
color rather than transparency.

The basic method is very simple:
   BufferedImage bi = ImageIO.read(srcFile) ;
   Image scaled = bi.getScaledInstance(targetWidth, targetHeight, 
Image.SCALE_FAST) ;
   ColorModel cm = bi.getColorModel() ;
   BufferedImage out = toBufferedImage1(bi, scaled, cm) ;
   ImageIO.write(out, "png", destFile);

the tricky part is converting the scaled image to a buffered image in order to 
write out the PNG.

I'm doing that like this:
   // Option 1 -- Using the screen as basis for gc
   GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
   GraphicsDevice gs = ge.getDefaultScreenDevice();
   GraphicsConfiguration gc=gs.getDefaultConfiguration();

   // Option 2 - using  the original image as basis for gc
   Graphics2D originalG = original.createGraphics() ;
   GraphicsConfiguration origDC=originalG.getDeviceConfiguration() ;

   BufferedImage bimage = origDC.createCompatibleImage(image.getWidth(null), 
image.getHeight(null), Transparency.BITMASK);

and then copy the scaled image into the new buffered image like this:
        Graphics g = bimage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();

If I use option 1 (based on the screen) I get a 24-bit PNG that has correct 
transparency.
If I use option 2 (based on the original image) I get an 8-bit PNG that is 
correct except all transparent fields are filled with an arbitrary, incorrect 
color.

What I want is an 8-bit PNG with correct transparency.

So, can anyone tell me how to:
     a) Write out an 8-bit PNG in another way that preserves transparency?
or  b) Tell me how to make a buffered 8-bit image fully transparent (before I 
paint over with the image data)?  I've tried various calls to setRGB and 
calling writableRaster w/o success.
or  c) Give me a different way to write out an 8-bit PNG that doesn't rely on 
BufferedImage?

Thanks in advance for any suggestions.

Doug
[Message sent by forum member 'seattledoug' (seattledoug)]

http://forums.java.net/jive/thread.jspa?messageID=117956

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to