Bombsquad.DynamicMedia.Imaging.BitmapTransforms.CropBitmapTransformerFactory.TryGetBitmapTransform C# (CSharp) Method

TryGetBitmapTransform() public method

public TryGetBitmapTransform ( string optionValue, string>.IEnumerable settings, BitmapSource>.Func &bitmapTransformerFunc ) : bool
optionValue string
settings string>.IEnumerable
bitmapTransformerFunc BitmapSource>.Func
return bool
        public bool TryGetBitmapTransform(string optionValue, IEnumerable<KeyValuePair<string, string>> settings, out Func<BitmapSource, BitmapSource> bitmapTransformerFunc)
        {
            var cropOriginFunc = GetCropPointFunc(GetSetting(settings, "cropOrigin", "center,center"));
            var cropSize = GetCropSize(optionValue);

            bitmapTransformerFunc = bitmapSource =>
            {
                double widthScale = cropSize.Width / bitmapSource.PixelWidth;
                double heightScale = cropSize.Height / bitmapSource.PixelHeight;
                double scale = Math.Max(widthScale, heightScale);

                bitmapSource = new TransformedBitmap(bitmapSource, new ScaleTransform(scale, scale));

                var cropOrigin = cropOriginFunc(bitmapSource);
                var x = cropOrigin.X - (cropSize.Width / 2);
                x = Math.Max(0, x);
                x = Math.Min(x, bitmapSource.PixelWidth - cropSize.Width);

                var y = cropOrigin.Y - (cropSize.Height / 2);
                y = Math.Max(0, y);
                y = Math.Min(y, bitmapSource.PixelHeight - cropSize.Height);

                return new CroppedBitmap(bitmapSource, new Int32Rect((int)Math.Round(x), (int)Math.Round(y), (int)cropSize.Width, (int)cropSize.Height));
            };

            return true;
        }