|
| 1 | +--- |
| 2 | +title: Simplifying Image Cropping and Saving in RadImageEditor |
| 3 | +description: Learn how to enable users to directly draw a rectangle on an image and save the cropped portion without prompts using RadImageEditor for ASP.NET AJAX. |
| 4 | +type: how-to |
| 5 | +page_title: Streamlining Image Cropping with RadImageEditor for ASP.NET AJAX |
| 6 | +slug: streamline-image-cropping-radimageeditor |
| 7 | +tags: radimageeditor, image cropping, asp.net ajax, client-side cropping, server-side saving |
| 8 | +res_type: kb |
| 9 | +ticketid: 1675772 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | +<tbody> |
| 16 | +<tr> |
| 17 | +<td>Product</td> |
| 18 | +<td>RadImageEditor for ASP.NET AJAX</td> |
| 19 | +</tr> |
| 20 | +<tr> |
| 21 | +<td>Version</td> |
| 22 | +<td>all</td> |
| 23 | +</tr> |
| 24 | +</tbody> |
| 25 | +</table> |
| 26 | + |
| 27 | +## Description |
| 28 | +Is it possible to crop a portion of an image by drawing a rectangle directly on it and then saving that cropped area to the server without any user prompts? This knowledge base article demonstrates how to simplify the image cropping process in the [RadImageEditor for ASP.NET AJAX](https://demos.telerik.com/aspnet-ajax/imageeditor/examples/crop/defaultcs.aspx), allowing users to directly select a portion of an image and save it with minimal interaction. |
| 29 | + |
| 30 | +This knowledge base article also answers the following questions: |
| 31 | +- How can I enable direct cropping in RadImageEditor? |
| 32 | +- What is the method to save a cropped image directly to the server in RadImageEditor? |
| 33 | +- Can the image cropping dialog in RadImageEditor be bypassed? |
| 34 | + |
| 35 | +## Solution |
| 36 | + |
| 37 | +To allow users to crop an image directly on the RadImageEditor and save the cropped portion without any prompts, follow these steps: |
| 38 | + |
| 39 | +1. **Hide the Crop Dialog**: Use CSS to hide the tools panel and enable direct interaction with the image for cropping. |
| 40 | + |
| 41 | +2. **Implement Client-Side Cropping**: Utilize custom JavaScript to capture the cropping rectangle drawn by the user and send the cropping parameters to the server. |
| 42 | + |
| 43 | +3. **Server-Side Image Saving**: Leverage the `ImageSaving` event of the RadImageEditor to save the cropped portion of the image to a specified location on the server. |
| 44 | + |
| 45 | +### Hiding the Crop Dialog and Implementing Direct Cropping |
| 46 | + |
| 47 | +Add the following markup and script to your ASPX page to hide the crop dialog and implement direct cropping: |
| 48 | + |
| 49 | +```html |
| 50 | +<style> |
| 51 | + #RadImageEditor1_ToolsPanel { |
| 52 | + display: none !important; |
| 53 | + } |
| 54 | +</style> |
| 55 | +<telerik:RadImageEditor ID="RadImageEditor1" runat="server" OnImageSaving="RadImageEditor1_ImageSaving" ImageManager-ViewPaths="~/Images" ImageManager-UploadPaths="~/Images" ImageUrl="~/Images/YourImage.jpg"> |
| 56 | + <Tools> |
| 57 | + <telerik:ImageEditorToolGroup> |
| 58 | + <telerik:ImageEditorTool CommandName="Crop" /> |
| 59 | + <telerik:ImageEditorTool CommandName="Save" /> |
| 60 | + </telerik:ImageEditorToolGroup> |
| 61 | + </Tools> |
| 62 | +</telerik:RadImageEditor> |
| 63 | +<script> |
| 64 | + function OnClientClicked(sender, args) { |
| 65 | + var imgEditor = $find("<%= RadImageEditor1.ClientID %>"); |
| 66 | + imgEditor.getEditableImage().crop(collectBounds()); |
| 67 | + imgEditor.saveImageOnServer("", true); |
| 68 | + } |
| 69 | +
|
| 70 | + function collectBounds() { |
| 71 | + // Implement the logic to collect the crop bounds based on user selection |
| 72 | + } |
| 73 | +</script> |
| 74 | +<telerik:RadButton runat="server" ID="RadButton1" Text="Save" AutoPostBack="false" OnClientClicked="OnClientClicked" /> |
| 75 | +``` |
| 76 | + |
| 77 | +### Handling Server-Side Image Saving |
| 78 | + |
| 79 | +Implement the `ImageSaving` event handler in your code-behind to save the cropped image: |
| 80 | + |
| 81 | +```c# |
| 82 | + protected void RadImageEditor1_ImageSaving(object sender, Telerik.Web.UI.ImageEditorSavingEventArgs args) |
| 83 | + { |
| 84 | + // Save the cropped image to disk or database |
| 85 | + Telerik.Web.UI.ImageEditor.EditableImage img = args.Image; |
| 86 | + img.Image.Save(Server.MapPath("~/Images/CroppedImage.jpg")); |
| 87 | + args.Cancel = true; // Cancel the default save action |
| 88 | + } |
| 89 | +``` |
| 90 | +```vb |
| 91 | +Protected Sub RadImageEditor1_ImageSaving(sender As Object, args As Telerik.Web.UI.ImageEditorSavingEventArgs) |
| 92 | + Dim img As Telerik.Web.UI.ImageEditor.EditableImage = args.Image |
| 93 | + img.Image.Save(Server.MapPath("~/Images/CroppedImage.jpg")) |
| 94 | + args.Cancel = True ' Prevent the default save action |
| 95 | +End Sub |
| 96 | +``` |
| 97 | + |
| 98 | +This setup streamlines the image cropping process in RadImageEditor, enabling users to easily select and save a portion of an image without navigating through additional dialogs or prompts. |
| 99 | + |
| 100 | +## See Also |
| 101 | + |
| 102 | +- [RadImageEditor Crop Tool Demo](https://demos.telerik.com/aspnet-ajax/imageeditor/examples/crop/defaultcs.aspx) |
| 103 | +- [RadImageEditor Client-Side Programming](https://docs.telerik.com/devtools/aspnet-ajax/controls/imageeditor/client-side-programming/overview) |
| 104 | +- [RadImageEditor Server-Side Programming](https://docs.telerik.com/devtools/aspnet-ajax/controls/imageeditor/server-side-programming/overview) |
0 commit comments