IUriStreamProvider Interface
Allows you to retirve data from a URI.
Namespace: DevExpress.Office.Services
Assembly: DevExpress.Office.v24.1.Core.dll
NuGet Packages: DevExpress.Office.Core, DevExpress.Win.Navigation
Declaration
Remarks
To ensure that you load images and not just references to these images, create a custom class that implements the IUriStreamProvider
interface. This applies to images loaded from:
- a database during the mail merge process
- an external source when an HTML or MHT file contains URIs to these images.
Create a custom class that implements the IUriStreamProvider
interface, and customize its GetStream
method. This method should return a stream with image data.
When your custom class is ready, call the IUriStreamService.RegisterProvider method to register this provider class.
Example
How to: Implement the IUriStreamProvider Service to Insert Images from a Data Source During Mail Merge
The code sample below shows the IUriStreamProvider
implementation used to insert images from a database. The INCLUDEPICTURE field in the template has a nested MERGEFIELD that refers to the EmployeeID field from the database. The GetStream method parses the received URI (the INCLUDEPICTURE field) and finds the required data row, and returns the MemoryStream with an image.
Note
Make sure that the MERGEFIELD field nested in the INCLUDEPICTURE field in the template refers to the data table’s primary key. Otherwise, the IUriStreamProvider
service cannot correctly find the required table row.
using DevExpress.Office.Services;
using System;
using System.Data;
using System.IO;
public class ImageStreamProvider : IUriStreamProvider
{
static readonly string prefix = "dbimg://";
DataTable table;
string columnName;
public ImageStreamProvider(DataTable sourceTable, string imageColumn)
{
this.table = sourceTable;
this.columnName = imageColumn;
}
public Stream GetStream(string uri)
{
// Parse the retrieved URI string
uri = uri.Trim();
if (!uri.StartsWith(prefix))
return null;
// Remove the prefix from the retrieved URI string
string strId = uri.Substring(prefix.Length).Trim();
int id;
// Check if the string contains the primary key
if (!int.TryParse(strId, out id))
return null;
// Retrieve the row that corresponds
// with the key
DataRow row = table.Rows.Find(id);
if (row == null)
return null;
// Convert the image string from this row
// to a byte array
byte[] bytes = Convert.FromBase64String(row[columnName] as string) as byte[];
if (bytes == null)
return null;
// Return the MemoryStream with an image
MemoryStream memoryStream = new MemoryStream(bytes);
return memoryStream;
}
}