In Development
Last Updated: 10 Jun 2024 17:38 by ADMIN
Scheduled for 2024 Q3 (August)
Eddie
Created on: 21 May 2024 08:39
Category: RichTextEditor
Type: Bug Report
0
RadRichTextEditor: Image losses transparency after being edited
1 comment
ADMIN
Nadya | Tech Support Engineer
Posted on: 21 May 2024 08:48

Hi, Eddie,

You can use the following workaround:

public class CustomImageEditDialog : ImageEditorDialog
{
    private RadButton okButton;

    public CustomImageEditDialog() : base()
    {
        this.okButton = new RadButton();
        this.okButton.Parent = this;
        RadButton oldOkButton = this.Controls.Find("radButton1", true)[0] as RadButton;

        this.okButton.Text = oldOkButton.Text;
        this.okButton.Location = oldOkButton.Location;
        this.okButton.Size = oldOkButton.Size;
        this.okButton.Click += OkButton_Click;

        oldOkButton.Hide();
    }

    private void OkButton_Click(object sender, EventArgs e)
    {
        using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
        {
            RadImageEditor radImageEditor1 = this.Controls.Find("radImageEditor1", true)[0] as RadImageEditor;

            radImageEditor1.ImageEditorElement.SaveImage(stream, System.Drawing.Imaging.ImageFormat.Png);
            Telerik.WinControls.RichTextEditor.UI.Size newSize = new Telerik.WinControls.RichTextEditor.UI.Size(radImageEditor1.CurrentBitmap.Size.Width, radImageEditor1.CurrentBitmap.Size.Height);

            Type type = typeof(ImageEditorDialog);
            var inlineField = type.GetField("originalInline", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            ImageInline originalInline = inlineField.GetValue(this) as ImageInline;

            var actionField = type.GetField("replaceCurrentInlineCallback", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            Action<Inline, Inline> replaceCurrentInlineCallback = actionField.GetValue(this) as Action<Inline, Inline>;

            ImageInline newInline = new ImageInline(stream, newSize, originalInline.Extension);
            replaceCurrentInlineCallback(originalInline, newInline);
        }

        this.DialogResult = DialogResult.OK;
    }

    protected override void OnClientSizeChanged(EventArgs e)
    {
        base.OnClientSizeChanged(e);

        var c = this.Controls.Find("radButton2", true);
        if (c == null || c.Length == 0)
        {
            return;
        }

        RadButton cancelButton = c[0] as RadButton;
        this.okButton.Left = cancelButton.Left - this.okButton.Width - 10;
        this.okButton.Top = cancelButton.Top;
        this.okButton.Height = cancelButton.Height;
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ImageEditorDialog d = new CustomImageEditDialog();
        this.radRichTextEditor1.RichTextBoxElement.ImageEditorDialog = d;
    }
}

Regards,
Nadya | Tech Support Engineer
Progress Telerik

A brand new ThemeBuilder course was just added to the Virtual Classroom. The training course was designed to help you get started with ThemeBuilder for styling Telerik and Kendo UI components for your applications. You can check it out at https://learn.telerik.com