Crop whitespace from PNG image

Nedeľa, Február 1st, 2009

Yesterday, I’ve posted about interesting AS3 Class InteractivePNG, and now I’m adding new addition for solving other problem with transparent Bitmaps. If you have transparent bitmap with whitespace around image and you want to crop whitespace around “real” image, you can use this function…

public static function getBitmapCroppedOutWhitespace(bd:BitmapData):Bitmap
{
	var bmd:BitmapData = new BitmapData(bd.width, bd.height, true, 0x00000000);
	bmd.draw(bd);
 
	var rect:Rectangle = bmd.getColorBoundsRect(0xFFFFFF, 0x00000000, false);
	var bmd2:BitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
	bmd2.draw(bmd, new Matrix(1,0,0,1, -rect.x, -rect.y));
 
	var bmp:Bitmap = new Bitmap(bmd2);
 
	return bmp;
}

From BitmapData it creates Bitmap without whitespace. It will find whitespace automaticaly and removes it. If you want to insert Bitmap data instead of BitmapData, just edit this function slightly. This is just idea how to do such cropping. Enjoy, I hope it will be useful for someone.


tagged under: ..

ABOUT THIS AUTHOR

2 Comments


  1. affordable web hosting
    Visit Site
    Február 3rd, 2009

    Ya there is alot to know about the web development industry. And sometimes it’s hard to find good information on the web. Thanks for the good input.


  2. Jos Faber
    Visit Site
    Február 9th, 2009

    Cool use of getColorBoundsRect. I was looking into this one as well. Made an AIR app that crops en entire dir with png’s and outputs a textfile with coordinates as well. Your script to get the whitespace out is better. It’s less code. I should implement it into mine ;-)

    here’s the source and compiled app: http://www.nrg3.nl/blog/?p=673

Sorry, comments for this entry are closed at this time.

Images is enhanced with WordPress Lightbox JS by Zeo