Image Scaling problems, with transparency *SOLVED*
Hello
I need to scale gif images, and wrote a scaling function that would take and image and two new dimensions and scale the image accordingly, say i do this on a gif image 16 * 16, this image is a circle, with transparent areas at the edges so it appears as a cirlce, this is fine, untill run it thru my imageFittedToSize methid, which scales it, but changes the transparent background to appear as black: i attach an example, the left image is the result, the right is the original which is transparent where seen as white.
Here is the function i am using:
Code:
/**
* This will create a scaled image, Note, we only reduce an image,
* NOT grow and image.
* @param image - Image to be Scaled Down
* @param width - the desire Width
* @param height - the desired Height.
* @return - the new Buffered Image.
*/
public static BufferedImage imageFittedToSize(Image image, int width, int height){
/**
* Get the Original Height and Width
*/
int origHeight = image.getHeight(null);
int origWidth = image.getWidth(null);
/**
* Work out how much we'd have to scale by to get the new dimensions.
*/
double heightScale = (double)height/(double)origHeight;
double widthScale = (double)width / (double)origWidth;
/**
* Decide which side need to be scaled more
* (so we keep the right ratio on height and width
*/
double scale = (widthScale < heightScale ? widthScale:heightScale);
/**
* Calculate the New Output Height and Width given the Scale Options.
*/
int outputWidth, outputHeight;
if(scale < 1.0d){
outputWidth = (int)(scale*origWidth);
outputHeight = (int)(scale*origHeight);
}else{
outputWidth = origWidth;
outputHeight = origHeight;
}
/**
* Create a new Buffered Image
*/
BufferedImage outImage = new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_RGB);
/**
* Set the Scale
*/
AffineTransform tx = new AffineTransform();
/**
* If the Image is smaller than the desired image Size
* lets not bother scaling.
*/
if(scale < 1.0d){
tx.scale(scale, scale);
}
/**
* Paint the Image.
*/
Graphics2D g2d = outImage.createGraphics();
g2d.drawImage(image, tx, null);
g2d.dispose();
/**
* Return the Scaled Image.
*/
return outImage;
}
I just can't seem to get rid of the black parts, i'm thinking its something trivial! at least lets hope so...
be grand if anyone had any ideas, suggestions
Andy