         return _indicesCorners;
     }
 
-    private static function bitmapsSameSize(a:BitmapData, b:BitmapData):Boolean
-    {
-        return a && b && a.width == b.width && a.height == b.height;
+    private static function clampRect(rect:Rectangle, outer:Rectangle):Rectangle
+    {
+        var newLeft:Number = clamp(rect.left, outer.left, outer.right);
+        var newRight:Number = clamp(rect.right, outer.left, outer.right);
+        var newTop:Number = clamp(rect.top, outer.top, outer.bottom);
+        var newBottom:Number = clamp(rect.bottom, outer.top, outer.bottom);
+
+        return new Rectangle(newLeft, newTop, newRight - newLeft, newBottom - newTop);
+    }
+
+    private static function clamp(v:Number, min:Number, max:Number):Number
+    {
+        return v < min ? min :
+               v > max ? max :
+                         v;
     }
 }
 }


    public function set bitmapData(new_bitmapData:BitmapData):void
    {
        uvsNeedRecalc = vertsNeedRecalc = !bitmapsSameSize(_bitmapData, new_bitmapData);
        _bitmapData = new_bitmapData;
        invalidate();
    }



    public override function set scale9Grid(innerRectangle:Rectangle):void
    {
        _scale9Grid = innerRectangle && _bitmapData ?
              clampRect(innerRectangle, _bitmapData.rect)
            : innerRectangle;
        uvsNeedRecalc = vertsNeedRecalc = true;
        invalidate();
    }
