--- In flexcoders@yahoogroups.com, Greg Hess <flexeff...@...> wrote:
>
> Hi All,
> 
> I have a content area that I need to fill with a user supplied
> graphic. The graphic may be any aspect ratio and I want to scale the
> image to the content area and maintain aspect ratio however, instead
> of producing "white" space where the aspect ratio could not be
> achieved(Standard behavior of the Image class) I would like to clip
> the image. The area will always be filled entirely, the aspect ratio
> will be maintained but the image may be clipped.
> 
> Does anyone know how I can do this?
> 
> Any help much appreciated.

This is a snippet from Flash, not Flex, but it might point you in the right 
direction:

var loader:Loader = Loader(e.target.loader);
                //measure the content
                var lw:int = loader.width;
                var lh:int = loader.height;
                var finishedWidth:int;
                var finishedHeight:int;
                
                /*      Check to see which dimension of the image is 
                        closer to the same thumbnail dimension.  We'll
                        scale that one and crop the other.      */
                if ( Math.abs(thumbwidth - lw) < Math.abs(thumbheight - lh) ) {
                        //scale horizontally
                        scaledX = true;
                        trace ('scaled x');
                } else {
                        //scale vertically
                        scaledX = false;
                        trace('scaled y');
                }
                scaleFactor = calculateScale(lw, lh, scaledX);
                trace('scalefactor', scaleFactor);
                //calculate what the dimensions will be after scale applied
                finishedWidth = Math.ceil(scaleFactor * lw);
                finishedHeight = Math.ceil(scaleFactor * lh);
                /*      In most cases, that will work, but in some cases that
                        gets it wrong.  Check for this and fix it       */
                if (finishedWidth < thumbwidth || finishedHeight < thumbheight) 
{
                        trace(scaleFactor * lw, scaleFactor * lh);
                        scaleFactor = calculateScale(lw, lh, !scaledX);
                        //recalculate finished dimensions
                        finishedWidth = Math.ceil(scaleFactor * lw);
                        finishedHeight = Math.ceil(scaleFactor * lh);
                }
                trace(loader.height*scaleFactor, loader.width*scaleFactor);
                var matrix:Matrix = new Matrix();
                matrix.createBox(scaleFactor, scaleFactor);
                //make a scaled copy of the content
                var scaledBitmap:BitmapData = new BitmapData(finishedWidth, 
finishedHeight, false);
                
                scaledBitmap.draw(loader.content, matrix, null, null, null, 
true);
                //crop the bitmap
                //this is a slice out of the middle of the image
                var rect:Rectangle = new 
Rectangle(Math.floor((finishedWidth-thumbwidth)/2),
                                                                 
Math.floor((finishedHeight-thumbheight)/2),
                                                                 
Math.ceil((finishedWidth +thumbwidth)/2),
                                                                 
Math.ceil((finishedHeight+thumbheight)/2));
                trace(Math.floor((finishedWidth-thumbwidth)/2),
                                 Math.floor((finishedHeight-thumbheight)/2),
                                 Math.ceil((finishedWidth +thumbwidth)/2),
                                 Math.ceil((finishedHeight+thumbheight)/2));
                trace('width', finishedWidth, 'height', finishedHeight);
                //move it into the corner
                var point:Point = new 
Point(Math.floor((finishedWidth-thumbwidth)/2)*-1, 
Math.floor((finishedHeight-thumbheight)/2) * -1);
                var croppedBitmap:BitmapData = new BitmapData(finishedWidth, 
finishedHeight, false);
                croppedBitmap.copyPixels(scaledBitmap, rect, point);
                
                var image:Bitmap = new Bitmap(croppedBitmap);
                addChild(image);
                imageLoadCount++;
                actualLoadCount++;
                image.x = (thumbwidth+10) * (actualLoadCount);
                image.y = 200;

HTH;

Amy

Reply via email to