Set crop rectangle from code

9 posts, 2 answers
  1. Robert
    Robert avatar
    31 posts
    Member since:
    Apr 2011

    Posted 17 Jun 2014 Link to this post

    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



  2. Answer
    Todor
    Admin
    Todor avatar
    168 posts

    Posted 20 Jun 2014 Link to this post

    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.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Robert
    Robert avatar
    31 posts
    Member since:
    Apr 2011

    Posted 20 Jun 2014 in reply to Todor Link to this post

    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. 

  5. Robert
    Robert avatar
    31 posts
    Member since:
    Apr 2011

    Posted 22 Jun 2014 in reply to Todor Link to this post

    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?



  6. Answer
    Todor
    Admin
    Todor avatar
    168 posts

    Posted 24 Jun 2014 Link to this post

    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.
     
  7. Robert
    Robert avatar
    31 posts
    Member since:
    Apr 2011

    Posted 26 Aug 2014 in reply to Todor Link to this post

    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.
  8. Todor
    Admin
    Todor avatar
    168 posts

    Posted 29 Aug 2014 Link to this post

    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.
     
  9. Robert
    Robert avatar
    31 posts
    Member since:
    Apr 2011

    Posted 10 Sep 2014 in reply to Todor Link to this post

    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.
  10. Todor
    Admin
    Todor avatar
    168 posts

    Posted 10 Sep 2014 Link to this post

    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.
     
Back to Top
UI for WPF is Visual Studio 2017 Ready