Inline image from email html

1 Answer 18 Views
RichTextEditor
Jure
Top achievements
Rank 2
Iron
Iron
Jure asked on 04 Oct 2021, 01:04 PM | edited on 06 Oct 2021, 11:32 AM

Hello.

I import a HTMLBody to a RadDocument. It's coming from Outlook.MailItem, so I also have attachments.

Dim provider As HtmlFormatProvider = New HtmlFormatProvider()
Dim doc = provider.Import(currentMail.HTMLBody)

The HTMLBody contains something like this:

<img src="cid:ii_kuchydn60" alt="logo.jpg" width="145" height="89">

So the displayed image element is just a rectangle with a red x.

Is there a way to also import an image / Outlook.Attachment to the document (other than saving the attachment and then modifying src property of img tag)?

Edit: based on Tanya's answer I was able to do this, it works but I'm not sure how safe it is:


 Private Sub LoadImageFromUrl(sender As Object, e As LoadImageEventArgs)

        Dim att = (From a As Attachment In currentAttachments Where a.FileName = e.ImageElement.Alt).FirstOrDefault

        If att IsNot Nothing Then
            Dim tempFile = IO.Path.GetTempPath + att.FileName
            att.SaveAsFile(tempFile)
            e.ImageElement.UriSource = New Uri(tempFile)
            e.Handled = True
        End If

    End Sub

Jure

1 Answer, 1 is accepted

Sort by
0
Accepted
Tanya
Telerik team
answered on 06 Oct 2021, 10:19 AM

Hi Jure,

You can use the LoadImageFromUrl event of the HtmlFormatProvider's ImportSettings to read the image and pass it to the import logic. The event allows you to set the UriSource of the image, so you will need to have it saved.

private void ImportSettings_LoadImageFromUrl(object sender, LoadImageEventArgs e)
{
    string uri = e.Url;
    // Modify uri to point to the image
    e.ImageElement.UriSource = new Uri(uri);
    e.Handled = true;
}

Hope this is helpful.

Regards,
Tanya
Progress Telerik

Remote troubleshooting is now easier with Telerik Fiddler Jam. Get the full context to end-users' issues in just three steps! Start your trial here - https://www.telerik.com/fiddler-jam.
Tanya
Telerik team
commented on 06 Oct 2021, 11:03 AM

You can also use the Init() method of ImageElement to pass a Stream instance containing the image.
Jure
Top achievements
Rank 2
Iron
Iron
commented on 06 Oct 2021, 11:33 AM

Thanks. I updated my question with a solution. I don't know how to get a Stream from the attachment without saving it first.
Tags
RichTextEditor
Asked by
Jure
Top achievements
Rank 2
Iron
Iron
Answers by
Tanya
Telerik team
Share this question
or