This is a migrated thread and some comments may be shown as answers.

Set crop rectangle from code

8 Answers 285 Views
ImageEditor
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 17 Jun 2014, 10:13 PM
In my WPF app, I need to take a bitmap I am given and extract two rectangles from it, and then display the original plus each extracted rectangle, each of which would first be saved to disk. 

I can display the bitmap ok in RadImageEditor and cause the crop rectangle to display using ExecuteTool(new CropTool), allowing manual selection, and after user interaction then save that cropped image ok.  

What I don't understand is how to set the crop rectangle from code.

I have discovered the CropCommandContext returned by ExecutingTool.GetContext, but that is readonly.  

thanks



8 Answers, 1 is accepted

Sort by
0
Accepted
Todor
Telerik team
answered on 20 Jun 2014, 10:10 AM
Hello Robert,

Thank you for contacting us and for the detailed explanation.

You can create a CropCommandContext object with the desired parameters and pass it to RadImageEditor's ExecuteCommand() method later, together with the CropCommand. 

For example, if you want to crop a rectangle with width and height  of 100 and starting point with coordinates (0,0) from a loaded image in the RadImageEditor, you can use the following code snippet:
CropCommandContext context = new CropCommandContext(0, 0, 100, 100);
this.ImageEditorUI.ImageEditor.ExecuteCommand(new CropCommand(), context);
where the "ImageEditorUI" is the given value of a RadImageEditorUI's  x:Name property in your XAML.

I hope this helps.
If you need further assistance, please get back to us.


Regards,
Todor
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Robert
Top achievements
Rank 1
answered on 20 Jun 2014, 10:06 PM
Thank you, this is just what I needed.  I had searched quite a bit for help on this topic, but hadn't discovered this info. 

0
Robert
Top achievements
Rank 1
answered on 23 Jun 2014, 04:09 AM
An additional issue related to programmatic crop tool use:

I see that when I initiate a CropTool from code I am getting a set of 3 buttons in the top of the RadImageEditor;  'OK, Cancel, and Reset'.  I don't want those buttons to be there, and in fact they are creating problems.  I am using the crop tool to extract a portion of the original image, but after the operation I want to restore the original image in the RadImageEditor.  

At the minimum I would need to use my own event handler to intercept that behavior, but it would be best if the buttons were not shown.  Is there a way to hide those buttons?  Is there a way to control their behavior?



0
Accepted
Todor
Telerik team
answered on 24 Jun 2014, 08:57 AM
Hello Robert,

The buttons you described are part of the ToolSettingsPanel control. Since these buttons are part controls and they are used in code behind for that control, you cannot remove them, but you still can set their Visibility property to Collapsed. For this purpose you should override ToolSettingsPanel's control template and apply the necessary changes there. 

Please note, that the ToolSettingsPanel control is used as a settings panel for all tools and the applied changes will affect other tools as well. 

If you have further questions, please do not hesitate to contact us again.


Regards,
Todor
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Robert
Top achievements
Rank 1
answered on 27 Aug 2014, 12:09 AM
I need a bit more help with this.

I need to let a user select a crop rectangle and then commit to the crop operation. That's working ok, but I don't quite see how to set the initial crop rectangle size. Instead it defaults to a starting size which has some margins on each side of the image in question.  I already have approximate locations that would minimize user interaction if I could set the rectangle size to start with.

In the previous answer above, I see how to create a CropCommandContext, but I don't want to use that to actually perform the crop, I want to use that to define the visible crop rectangle starting size.      

Thanks.
0
Todor
Telerik team
answered on 29 Aug 2014, 10:36 AM
Hi Robert,

The CropTool gives the opportunity to crop a given area from a given image. You can adjust the crop area by setting its FixedSize, AspectRatio or InitialSize properties.

You can set these properties to the CropTool in XAML or in code-behind. To set the InitialSize property, see the following code snippet:
CropTool cropTool = new CropTool();
cropTool.InitialSize = new Size(150, 150);

You can also check our Silverlight Cropping demo which reveals more about the above mentioned CropTool functionality as well.

I hope this is helpful.
If you further questions, please get back to us.

Regards,
Todor
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Robert
Top achievements
Rank 1
answered on 10 Sep 2014, 05:20 AM
I just tried this and realized that Initialize is not a method on CropTool.   (Note I am using WPF)

 'Telerik.Windows.Media.Imaging.Tools.CropTool' does not contain a definition for 'InitialSize' 

Also, btw the 'Silverlight Cropping Demo' link isn't working.
0
Todor
Telerik team
answered on 10 Sep 2014, 06:38 AM
Hello Robert,

First of all, accept my apologies for the broken link. Here you can find the Silverlight Cropping demo.

The mentioned extended Crop tool functionality is shipped with our 2014 Q2 SP1 release (assembly version - 2014.2.729). If you are using older assembly version, this is the reason that you get the "'Telerik.Windows.Media.Imaging.Tools.CropTool' does not contain a definition for 'InitialSize'" message.

I'll be glad to assist you if you have further questions.

Regards,
Todor
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
ImageEditor
Asked by
Robert
Top achievements
Rank 1
Answers by
Todor
Telerik team
Robert
Top achievements
Rank 1
Share this question
or