Can I set a default pen color and thickness?

I would like to have the pen color and thickness pre-selected for the user. They should be able to click the pen, and have it mark in that color and size, without having to select them. I couldn't find how to do this. I tried something like:

        ImageEditorShapeSettings defaultSettings = new ImageEditorShapeSettings()
        {
            Color = Colors.Yellow,
            StrokeThickness = 4
        };

        MyImageEditor.AddShape(AnnotationShape.Pen, defaultSettings);

But I don't really want to add a shape, and I don't know how else I might set these values for the editor.

Thanks in advance.


5 Replies

SR Sri Radhesh Nag Subash Sankar Syncfusion Team May 30, 2025 11:41 AM UTC

Hi Stan,

  

Thank you for contacting us.


Regarding the setting of pen color and thickness pre-selected in SfImageEditor. You can achieve this by using ‘ AnnotationSelected’ Event of SfImageEditor.


Example code snippet:

private void OnAnnotationSelected(object sender, AnnotationSelectedEventArgs e)

{

 

    if (e.AnnotationSettings is ImageEditorShapeSettings shapeSettings)

    {

        if (shapeId ==  2)

        {

            shapeSettings.Color = Colors.Red;

            shapeSettings.StrokeThickness = 10;

        }

    }

}


We have prepared a simple sample demonstrating this. Please check the attached sample for your reference.


For more detailed information on Annotation Selected event please refer to the following KB article and ug document:


https://help.syncfusion.com/maui/imageeditor/events?cs-save-lang=1&cs-lang=xaml#annotation-selected-event


https://support.syncfusion.com/kb/article/15136/how-to-change-pen-annotation-default-color-in-the-net-maui-image-editor



If you have any further questions, please don’t hesitate to reach out!


Regards,

Sri Radhesh Nag S


Attachment: ImageEditor_4a0ffec9.zip


SL Stan Logan June 2, 2025 11:21 AM UTC

Thank you very much.

For future viewers, the 'shapeId' variable isn't necessary, and if you want to also set the color for text annotations, you could use code like this:

        if (e.AnnotationSettings is ImageEditorShapeSettings shapeSettings)
        {
            shapeSettings.Color = Colors.Yellow;
            shapeSettings.StrokeThickness = 6;
        }
        else if (e.AnnotationSettings is ImageEditorTextSettings textSettings)
        {
            textSettings.TextStyle.TextColor = Colors.Yellow;
        }

Thanks again.



SR Sri Radhesh Nag Subash Sankar Syncfusion Team June 3, 2025 12:37 PM UTC

Hi Stan,


We are glad that your query has been resolved! Please let us know if you need further assistance. As always, we are happy to help you out.


Regards,

Sri Radhesh Nag S 



SL Stan Logan June 3, 2025 06:07 PM UTC

I wanted to update my previous response. The code I posted worked insofar as I was able to set the defaults as desired. But I had not taken a closer look at the provided example project, and I have now discovered what the 'shapeId' variable is for.

In the example project, the ToolbarItemSelected even is handled, and the shapeId is a hard-coded number that is set when the 'Pen' is the selected toolbar item.

Specifying this object in some way prevents the OnAnnotationSelected code from setting the properties on _any_ shape, such as a text area.

If you do not specify this, the default color is applied in situations where you do not want it, such as when an existing text are is selected.

In short, look at the sample project for a better understanding of how this should be done.



TG Tamilarasan Gunasekaran Syncfusion Team June 4, 2025 01:36 PM UTC

Hi Stan,

We appreciate you revisiting the example and sharing your findings. This will certainly help others avoid similar issues. If you need any further clarification or assistance with any other aspect, please don't hesitate to let us know. We are here to help.


Regards,
Tamilarasan G.


Loader.
Up arrow icon