This is a migrated thread and some comments may be shown as answers.

HtmlFormatProvider - load local images from html

2 Answers 282 Views
RichTextEditor
This is a migrated thread and some comments may be shown as answers.
Sergey
Top achievements
Rank 2
Sergey asked on 14 Feb 2020, 10:17 AM

Hi, 

Can I load pictures via LoadImageFromUrl from a local disk without async methods?

The code below works, but it works poorly and often the images do not load, or not all are loaded.

Uri uri = new Uri(Path.Combine(Holder.TempPath, e.Url), UriKind.Absolute);

WebClient client = new WebClient();
client.OpenReadCompleted += (w, a) =>
    {
     if(a.Error == null)
      {
    try
     {
             e.ImageElement.Init(a.Result, new Telerik.WinControls.RichTextEditor.UI.Size(16, 16), extension);
     }
      catch
     {
       //Handle errors
      }
    }
    };
    client.OpenReadAsync(uri);

2 Answers, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 18 Feb 2020, 12:51 PM
Hello, Sergey,

The HtmlFormatProvider.LoadImageFromUrl was introduced to handle situations in which the images don't automatically load from URLs. It is necessary to initialize the image elements with the locally stored images as it is demonstrated below. Hence, download the images first and then import the document: 
public RadForm1()
{
    InitializeComponent();
 
    HtmlFormatProvider provider = new HtmlFormatProvider();
    provider.ImportSettings.LoadImageFromUrl += ImportSettings_LoadImageFromUrl;
    using (FileStream inputStream = File.OpenRead(@"..\..\Sample.htm"))
    {
        this.radRichTextEditor1.Document = provider.Import(inputStream);
    }
}
 
private void ImportSettings_LoadImageFromUrl(object sender, LoadImageEventArgs e)
{
    e.Handled = true;
    Telerik.WinControls.RichTextEditor.UI.BitmapImage img = new Telerik.WinControls.RichTextEditor.UI.BitmapImage();
    using (FileStream source = File.Open(@"..\..\test.png", FileMode.Open))
    {
        e.ImageElement.Init(source, "png");
    }
 
}

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Sergey
Top achievements
Rank 2
answered on 19 Feb 2020, 07:42 AM

Hello Dess,

Thanks, I didn’t even think about the Handle variable.

Tags
RichTextEditor
Asked by
Sergey
Top achievements
Rank 2
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Sergey
Top achievements
Rank 2
Share this question
or