ImageCrop
in namespace DotVVM.BusinessPack.Controls
Renders an ImageCrop control allowing to zoom, rotate and crop images.
Usage & Scenarios
A control which lets the user to make basic edits (like cropping, resizing or rotating) with a specified image.
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:
Resizerepresents the scale ratio of the image.Rotaterepresents represents the rotation angle.Croprepresents a rectangular area (withLeft,Top,WidthandHeightproperties) the crop settings.Roundrepresents 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;
using DotVVM.Framework.ViewModel;
namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample1
{
public class ViewModel : DotvvmViewModelBase
{
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;
using DotVVM.Framework.ViewModel;
namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample2
{
public class ViewModel : DotvvmViewModelBase
{
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;
using DotVVM.Framework.ViewModel;
namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample3
{
public class ViewModel : DotvvmViewModelBase
{
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;
using DotVVM.Framework.ViewModel;
namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample4
{
public class ViewModel : DotvvmViewModelBase
{
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;
using DotVVM.Framework.ViewModel;
namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample5
{
public class ViewModel : DotvvmViewModelBase
{
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;
using DotVVM.Framework.ViewModel;
namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample6
{
public class ViewModel : DotvvmViewModelBase
{
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:
SwitchToMovingIconCssClassallows to customize the switch mode icon. The default value isfa fa-arrows(FontAwesome).SwitchToCroppingIconCssClassallows to customize the switch mode icon. The default value isfa fa-crop(FontAwesome).ResetIconCssClassallows to customize the reset icon. The default value isfa fa-refresh(FontAwesome).RotateLeftIconCssClassallows to customize the rotate left icon. The default value isfa fa-rotate-left(FontAwesome).RotateRightIconCssClassallows to customize the rotate right icon. The default value isfa fa-rotate-right(FontAwesome).ZoomInIconCssClassallows to customize the zoom in icon. The default value isfa fa-search-plus(FontAwesome).ZoomOutIconCssClassallows to customize the zoom out icon. The default value isfa 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;
using DotVVM.Framework.ViewModel;
namespace DotvvmWeb.Views.Docs.Controls.businesspack.ImageCrop.sample7
{
public class ViewModel : DotvvmViewModelBase
{
public string ImagePath { get; set; } = "/Resources/Images/picture.jpg";
public ImageOperations ImageOperations { get; set; } = new ImageOperations();
}
}
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
static value
|
null | |
| Attributes | Dictionary<String,Object> |
attribute
static value
|
null | ||
| ClientIDMode | ClientIDMode | Gets or sets the client ID generation algorithm. |
attribute
static value
|
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
static value
bindable
|
null | |
| 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
static value
|
null | |
| 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
static value
|
null | |
| 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
static value
|
null | |
| ID | String | Gets or sets the unique control ID. |
attribute
static value
|
null | |
| ImageUrl | String | Gets or sets the URL of an image to crop. |
attribute
static value
bindable
|
null | |
| InnerText | String | Gets or sets the inner text of the HTML element. |
attribute
static value
bindable
|
null | |
| Operations | ImageOperations | Gets or sets the operations applied to the image being cropped. |
attribute
bindable
|
null | |
| ResetIconCssClass | String | Gets or sets the CSS class for the icon displayed on the reset button. The default value is FaRefresh. |
attribute
static value
|
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
static value
|
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
static value
|
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
static value
|
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
static value
|
fa fa-arrows | |
| Visible | Boolean | Gets or sets whether the control is visible. |
attribute
bindable
|
True | |
| ZoomInIconCssClass | String | Gets or sets the CSS class for the icon displayed on the zoom-in button. The default value is FaSearchPlus. |
attribute
static value
|
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
static value
|
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. |