HtmlFormatProvider - load local images from html

3 posts, 1 answers
  1. Sergey
    Sergey avatar
    7 posts
    Member since:
    Jan 2020

    Posted 14 Feb Link to this post

    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. Answer
    Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3906 posts

    Posted 18 Feb Link to this post

    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.
  3. Sergey
    Sergey avatar
    7 posts
    Member since:
    Jan 2020

    Posted 19 Feb in reply to Dess | Tech Support Engineer, Sr. Link to this post

    Hello Dess,

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

Back to Top