Resizing an image upon save

I am trying to resize an image on Saving;

I have the sfImagEditor defined in the view like this :


    <ContentPage.Content x:DataType="local:CameraViewModel">

            <imageEditor:SfImageEditor

                    x:Name="Editor"

                   ImageSaving="SfImageEditor_ImageSaving"

                   Source="{Binding Picture}" />

    </ContentPage.Content>


in the code behind I have this :


        private void SfImageEditor_ImageSaving(object sender, Syncfusion.SfImageEditor.XForms.ImageSavingEventArgs args)

        {

            var photo = ImageSource.FromStream(() => args.Stream);

            string photoAsString = _photoService.ToBase64String(photo);

            MessagingCenter.Send<CameraEditView, string>(this, "NewImage", photoAsString);

            args.Cancel = true;

            _navigationService.GoBackAsync();

        }


I see that one can pass size parameters on the Save method :

How do I do this when the save method is called from the save button in the sfImage.Editor control





1 Reply

SS Sridevi Sivakumar Syncfusion Team July 2, 2021 09:33 AM UTC

Hi Christiaan Krause,

Greetings from Syncfusion.

We have achieved your requirement by calling the editor.save() method in image editor saving handler as per the below code snippet

Code snippet:

[XAML]:
 
 <imageeditor:SfImageEditor Source="{Binding Image}" x:Name="editor" ImageSaving="editor_ImageSaving"  /> 
 
 
[C#]: 
    
    public partial class MainPage : ContentPage 
    { 
        bool isImageSaved; 
        public MainPage() 
        { 
            InitializeComponent(); 
            editor.ToolbarSettings.ToolbarItemSelected += ToolbarSettings_ToolbarItemSelected; 
        } 
  
        private void ToolbarSettings_ToolbarItemSelected(object sender, Syncfusion.SfImageEditor.XForms.ToolbarItemSelectedEventArgs e) 
        { 
            if(e.ToolbarItem.Name=="Save") 
            { 
              /// When we set the isImageSaved to false, the added code in the image saving event will work. 
                isImageSaved = false; 
            } 
        } 
  
        private void editor_ImageSaving(object sender, Syncfusion.SfImageEditor.XForms.ImageSavingEventArgs args) 
        { 
            if(!isImageSaved) 
            { 
               //// Restrict the saving when you click the Save button  
                args.Cancel = true; 
                 
                  isImageSaved = true; 
  
               //// Image will save in mentioned size 
                editor.Save(".png", new Size(213, 764)); 
            } 
        } 
    } 
 

Let us know if you need any further assistance.

Regards
Sridevi S.  
 
 
 


Loader.
Up arrow icon