public static BitmapImage Clip(this BitmapSource bitmapSource,
                                       double left, double top, double width, double height)
        {
            Int32Rect rect = new Int32Rect((int)left, (int)top, (int)width, (int)height);

            if (CanClip(bitmapSource, rect))
            {
                CroppedBitmap bm = new CroppedBitmap(bitmapSource, rect);
                return(bm.ToBitmapImage());
            }
            return(null);
        }
        public static BitmapImage Clip(this BitmapSource bitmapSource,
                                       Rectangle r)
        {
            int top    = Math.Max(r.Y, 0);
            int left   = Math.Max(r.X, 0);
            int bottom = Math.Min(r.Y + r.Height, (int)bitmapSource.Height);
            int right  = Math.Min(r.X + r.Width, (int)bitmapSource.Width);

            Int32Rect rect = new Int32Rect(left, top, right - left, bottom - top);

            if (CanClip(bitmapSource, rect))
            {
                CroppedBitmap bm = new CroppedBitmap(bitmapSource, rect);

                return(bm.ToBitmapImage());
            }
            return(null);
        }