How to Set the Min and Max Crop Size for the Image in RadImageEditor
Environment
Product Version | 2018.2.620 |
Product | RadImageEditor for WPF |
Description
How to define the minimum and maximum size for the CropTool in RadImageEditor.
Solution
- Create a custom tool deriving from
CropTool
. - Create a parameterless constructor and use reflection to get the
CropAdorner
control. - Susbcribe to the
CropRectChanged
event of CropAdorner. - In the event handler limit the size of the adorner, by setting its
CropRect
property. You can expose additional properties on the custom crop tool class which can hold the min and max sizes.
public class CustomCropTool : CropTool
{
public Size MaxCropSize { get; set; }
public Size MinCropSize { get; set; }
private CropAdorner cropAdorner;
public CustomCropTool() : base()
{
var fieldInfo = typeof(CropTool).GetField("cropAdorner", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
this.cropAdorner = fieldInfo.GetValue(this) as CropAdorner;
this.cropAdorner.CropRectChanged += CropAdorner_CropRectChanged;
}
private void CropAdorner_CropRectChanged(object sender, EventArgs e)
{
var currentRect = this.cropAdorner.CropRect;
var w = Math.Max(this.MinCropSize.Width, Math.Min(this.MaxCropSize.Width, currentRect.Size.Width));
var h = Math.Max(this.MinCropSize.Height, Math.Min(this.MaxCropSize.Height, currentRect.Size.Height));
this.cropAdorner.CropRect = new Rect(currentRect.Location, new Size(w, h));
}
}
<telerik:ImageToolItem ImageKey="Crop" Text="Crop" Command="commands:ImageEditorRoutedCommands.ExecuteTool">
<telerik:ImageToolItem.CommandParameter>
<local:CustomCropTool x:Name="cropTool" MaxCropSize="300, 300" MinCropSize="100, 100" InitialSize="200, 200" />
</telerik:ImageToolItem.CommandParameter>
</telerik:ImageToolItem>