This example illustrates how to obtain an image referenced with the 'cid' prefix from an external file in BMP format.
The Rich Text Editor allows you to import HTML files that contain embedded images or links to external images with the src attribute specified as an image URL. However, you can have web files where images are referenced in a custom manner (for example, with the prefix 'cid' in the img src attribute, as in email files). In this case, you need to implement and register a custom IUriStreamProvider to import these files into the Rich Text Editor.
Files to Review
- Program.cs (VB: Program.vb)
More Examples
Documentation
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using DevExpress.Office.Services;
using DevExpress.Security;
using DevExpress.XtraRichEdit;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace RichIUriStreamProviderExample {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
private static string basePath = Directory.GetCurrentDirectory() + @"\TestDocs\";
static void Main(string[] args) {
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
// Custom IUriStreamProvider registration
IUriStreamService uriStreamService = wordProcessor.GetService<IUriStreamService>();
uriStreamService.RegisterProvider(new CustomUriStreamProvider(basePath, "bmp"));
wordProcessor.LoadDocument(basePath + "test.html");
wordProcessor.SaveDocument("Result.docx", DocumentFormat.OpenXml);
}
//Open the result
var p = new Process();
p.StartInfo = new ProcessStartInfo(@"Result.docx")
{
UseShellExecute = true
};
p.Start();
}
}
public class CustomUriStreamProvider : IUriStreamProvider
{
private string basePath;
private string imageExtension;
public string BasePath { get { return basePath; } set { basePath = value; } }
public string ImageExtension { get { return imageExtension; } set { imageExtension = value; } }
public CustomUriStreamProvider(string basePath, string imageExtension)
{
BasePath = basePath;
ImageExtension = imageExtension;
}
public Stream GetStream(string url)
{
string fileName = string.Format("{0}.{1}", url.Replace("cid:", string.Empty), ImageExtension);
MemoryStream memoryStream = new MemoryStream();
using (Image image = Image.FromFile(BasePath + fileName))
image.Save(memoryStream, ImageFormat.Bmp);
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream;
}
}
}