1

I am using the following code to "crop an image", however it ignores transparency, so any BufferedImages obtained from this method are completely opaque, and there don't appear to be any .getARGB() or .setARGB() methods. How do I work around this?

        private static BufferedImage getCroppedImage(BufferedImage wholeImage, int xPos, int yPos, int width, int height)
        {
            GraphicsEnvironment graphEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
            BufferedImage croppedImage = null;

            try
                {
                    GraphicsDevice screen = graphEnv.getDefaultScreenDevice();
                    GraphicsConfiguration gc = screen.getDefaultConfiguration();
                    croppedImage = gc.createCompatibleImage(width, height, Transparency.BITMASK);
                }
            catch (Exception e)
                {
                    new errorWindow(e, "crop, in Images");
                }

            if (croppedImage == null)
                {
                    croppedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                }

            int[] pixels = new int[width * height];
            wholeImage.getRGB(xPos, yPos, width, height, pixels, 0, width);
            croppedImage.setRGB(0, 0, width, height, pixels, 0, width);

            return croppedImage;
        }
Troyseph
  • 4,960
  • 3
  • 38
  • 61
  • 1
    Try using [`Transparency.TRANSLUCENT`](http://download.oracle.com/javase/6/docs/api/java/awt/Transparency.html#TRANSLUCENT) instead. – mre Jul 13 '11 at 18:12
  • No Difference, I need some way of getting and setting an integer value for transparency per pixel, the format of the BufferedImage is fine (.png) – Troyseph Jul 14 '11 at 12:27
  • 1
    SORRY @mre was correct, Transparency.TRANSLUCENT was the way forewards, I simply failed to change it in both places! – Troyseph Aug 04 '11 at 12:11

1 Answers1

1

Use Transparency.TRANSLUCENT instead. This will not ignore alpha values.

mre
  • 43,520
  • 33
  • 120
  • 170