DOT

VVM

 DOCS

Version: 1.0 | 1.1 |

ImageCrop

in namespace DotVVM.BusinessPack.Controls

Renders an ImageCrop control allowing to zoom, rotate and crop images.

Properties

Name Type Description Notes Default Value
AspectRatio String Gets or sets the aspect ratio to force when cropping the image (eg. 1:1).
attribute
inner element
static value
bindable
default
Attributes Dictionary<String,Object>
attribute
inner element
static value
bindable
default
ClientIDMode ClientIDMode Gets or sets the client ID generation algorithm.
attribute
inner element
static value
bindable
default
Static
DataContext Object Gets or sets a data context for the control and its children. All value and command bindings are evaluated in context of this value.
attribute
inner element
static value
bindable
default
ForcedCropHeight Int32? Gets or sets the forced height of the cropped image. If the ForcedCropWidth property is not set, it is computed automatically (see the AspectRatio).
attribute
inner element
static value
bindable
default
ForcedCropRadius Int32? Gets or sets the forced radius of the image corners. The value must be between 0 and 50 (both inclusive). It is not set by default.
attribute
inner element
static value
bindable
default
ForcedCropWidth Int32? Gets or sets the forced width of the cropped image. If the ForcedCropHeight property is not set, it is computed automatically (see the AspectRatio).
attribute
inner element
static value
bindable
default
ID String Gets or sets the unique control ID.
attribute
inner element
static value
bindable
default
ImageUrl String Gets or sets the URL of an image to crop.
attribute
inner element
static value
bindable
default
InnerText String Gets or sets the inner text of the HTML element.
attribute
inner element
static value
bindable
default
Operations ImageOperations Gets or sets the operations applied to the image being cropped.
attribute
inner element
static value
bindable
default
ResetIconCssClass String Gets or sets the CSS class for the icon displayed on the reset button. The default value is FaRefresh.
attribute
inner element
static value
bindable
default
fa fa-refresh
RotateLeftIconCssClass String Gets or sets the CSS class for the icon displayed on the rotate-left button. The default value is FaRotateLeft.
attribute
inner element
static value
bindable
default
fa fa-rotate-left
RotateRightIconCssClass String Gets or sets the CSS class for the icon displayed on the rotate-right button. The default value is FaRotateRight.
attribute
inner element
static value
bindable
default
fa fa-rotate-right
SwitchToCroppingIconCssClass String Gets or sets the CSS class for the icon displayed on the button switching from Cropping mode to Moving mode. The default value is FaCrop.
attribute
inner element
static value
bindable
default
fa fa-crop
SwitchToMovingIconCssClass String Gets or sets the CSS class for the icon displayed on the button switching from Cropping mode to Moving mode. The default value is FaArrows.
attribute
inner element
static value
bindable
default
fa fa-arrows
Visible Boolean Gets or sets whether the control is visible.
attribute
inner element
static value
bindable
default
True
ZoomInIconCssClass String Gets or sets the CSS class for the icon displayed on the zoom-in button. The default value is FaSearchPlus.
attribute
inner element
static value
bindable
default
fa fa-search-plus
ZoomOutIconCssClass String Gets or sets the CSS class for the icon displayed on the zoom-out button. The default value is FaSearchMinus.
attribute
inner element
static value
bindable
default
fa fa-search-minus

Events

Name Type Description
Changed Command Gets or sets the command that will be triggered when the image attributes are changed.

Usage & Scenarios

A control which lets the user to make basic edits (like cropping, resizing or rotating) with a specified image.

HTML Rendered by the Control

Sample 1: Basic Usage

The ImageUrl property specifies the URL of the image that is being edited.

The Operations property must be bound to a property of type ImageOperations in the viewmodel. This object contains the following properties:

  • Resize represents the scale ratio of the image.

  • Rotate represents represents the rotation angle.

  • Crop represents a rectangular area (with Left, Top, Width and Height properties) the crop settings.

  • Round represents the radius of the rounded corners.

You can use the DefaultImageFactory class to load the image, apply the ImageOperations to it and save the result image.

<bp:ImageCrop ImageUrl="{value: ImagePath}"
              Operations="{value: ImageOperations}" />

<p>Left: {{value: ImageOperations.Crop.Left}}px</p>
<p>Top: {{value: ImageOperations.Crop.Top}}px</p>
<p>Width: {{value: ImageOperations.Crop.Width}}px</p>
<p>Height: {{value: ImageOperations.Crop.Height}}px</p>
<p>Rotate: {{value: ImageOperations.Rotate}}°</p>
<p>Resize: {{value: ImageOperations.Resize}}px</p>

<bp:Button Text="Save"
           Click="{command: Save()}" />
<br />
<br />

<p>Result: <a href="{value: Result}">{{value: Result}}</a></p>
<img src="{value: Result}" />
using DotVVM.BusinessPack.Controls;
using DotVVM.Framework.Utils;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample1
{
    public class ViewModel
    {
        public string ImagePath { get; set; } = "/Resources/Images/picture.jpg";
        public string Result { get; set; }
        public ImageOperations ImageOperations { get; set; } = new ImageOperations();

        public void Save()
        {
            Result = $"/App_Images/{SecureGuidGenerator.GenerateGuid()}.png";

            using (var factory = new DefaultImageFactory())
            {
                factory
                    .Load(GetPhysicalPath(ImagePath))
                    .Apply(ImageOperations)
                    .Save(GetPhysicalPath(Result));
            }
        }

        private string GetPhysicalPath(string url)
        {
            return Context.Configuration.ApplicationPhysicalPath + url.Substring(1).Replace("/", "\\");
        }
    }
}

Sample 2: Aspect Ratio

The ratio between the width and height of the cropped area can be set using the AspectRatio property. The value of the property is string and the control expects two numbers separated by a colon, e.g. 1:1, 4:3, 16:9 etc.

<bp:ImageCrop ImageUrl="{value: ImagePath}"
              Operations="{value: ImageOperations}"
              AspectRatio="1:1" />
using DotVVM.BusinessPack.Controls;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample2
{
    public class ViewModel
    {
        public string ImagePath { get; set; } = "/Resources/Images/picture.jpg";
        public ImageOperations ImageOperations { get; set; } = new ImageOperations();
    }
}

Sample 3: Forced Crop Radius

To make the border radius fixed, the ForcedCropRadius property can be used. It expects a value between 0 and 50.

0 means no border radius. 50 means maximum border radius. The value is in percent, so the border radius of 50% can be used to make a circle.

<bp:ImageCrop ImageUrl="{value: ImagePath}"
              Operations="{value: ImageOperations}"
              ForcedCropRadius="50" />
using DotVVM.BusinessPack.Controls;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample3
{
    public class ViewModel
    {
        public string ImagePath { get; set; } = "/Resources/Images/picture.jpg";
        public ImageOperations ImageOperations { get; set; } = new ImageOperations();
    }
}

Sample 4: Forced Crop Area

The crop area dimensions can be fixed using the ForcedCropWidth and ForcedCropHeight properties.

<bp:ImageCrop ImageUrl="{value: ImagePath}"
              Operations="{value: ImageOperations}"
              ForcedCropWidth="150"
              ForcedCropHeight="150" />
using DotVVM.BusinessPack.Controls;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample4
{
    public class ViewModel
    {
        public string ImagePath { get; set; } = "/Resources/Images/picture.jpg";
        public ImageOperations ImageOperations { get; set; } = new ImageOperations();
    }
}

Sample 5: Changed Event

When the user changes any parameter of the image, the Changed event can be used to call a command in the viewmodel.

<bp:ImageCrop ImageUrl="{value: ImagePath}"
              Operations="{value: ImageOperations}"
              Changed="{command: Changed()}" />

<p>Change count: {{value: ChangeCount}}</p>
using DotVVM.BusinessPack.Controls;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample5
{
    public class ViewModel
    {
        public int ChangeCount { get; set; }
        public string ImagePath { get; set; } = "/Resources/Images/picture.jpg";
        public ImageOperations ImageOperations { get; set; } = new ImageOperations();

        public void Changed()
        {
            ChangeCount++;
        }
    }
}

Sample 6: Predefined Operations

If you set the ImageOperations parameters from the viewmodel, you can specify default values for the control.

<bp:ImageCrop ImageUrl="{value: ImagePath}"
              Operations="{value: ImageOperations}" />
using DotVVM.BusinessPack.Controls;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample6
{
    public class ViewModel
    {
        public string ImagePath { get; set; } = "/Resources/Images/picture.jpg";

        public ImageOperations ImageOperations { get; set; } = new ImageOperations {
            Crop = new CropRectangle {
                Left = 150,
                Top = 150,
                Width = 100,
                Height = 100
            },
            Resize = 400,
            Rotate = 45,
            Round = 50
        };
    }
}

Sample 7: Custom Icons

To customize the look of toolbar icons in the control, you can use the following properties:

  • SwitchToMovingIconCssClass allows to customize the switch mode icon. The default value is fa fa-arrows (Fontawesome).
  • SwitchToCroppingIconCssClass allows to customize the switch mode icon. The default value is fa fa-crop (Fontawesome).
  • ResetIconCssClass allows to customize the reset icon. The default value is fa fa-refresh (Fontawesome).
  • RotateLeftIconCssClass allows to customize the rotate left icon. The default value is fa fa-rotate-left (Fontawesome).
  • RotateRightIconCssClass allows to customize the rotate right icon. The default value is fa fa-rotate-right (Fontawesome).
  • ZoomInIconCssClass allows to customize the zoom in icon. The default value is fa fa-search-plus (Fontawesome).
  • ZoomOutIconCssClass allows to customize the zoom out icon. The default value is fa fa-search-minus (Fontawesome).
<bp:ImageCrop ImageUrl="{value: ImagePath}"
              Operations="{value: ImageOperations}"
              ResetIconCssClass="refresh"
              RotateLeftIconCssClass="rotate-left"
              RotateRightIconCssClass="rotate-right"
              ZoomInIconCssClass="plus"
              ZoomOutIconCssClass="minus" />
using DotVVM.BusinessPack.Controls;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample7
{
    public class ViewModel
    {
        public string ImagePath { get; set; } = "/Resources/Images/picture.jpg";
        public ImageOperations ImageOperations { get; set; } = new ImageOperations();
    }
}

Suggestions


Help & Contribute

The documentation is maintained on GitHub.
We'll be happy if you edit the page and create a pull request.

Send message