Skip to main content

IUriStreamProvider Interface

Allows you to retrieve data from a URI.

Namespace: DevExpress.Office.Services

Assembly: DevExpress.Office.v25.2.Core.dll

Declaration

[ComVisible(true)]
public interface IUriStreamProvider

Remarks

Create the IUriStreamProvider implementation to ensure that you load images and not just references to these images. 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(String) method. This method returns a stream with image data.

Call the IUriStreamService.RegisterProvider method to register the provider class.

Example

How to: Implement the IUriStreamProvider Service to Insert Images from a Data Source During Mail Merge

The following code snippet shows the IUriStreamProvider implementation used to insert images from a database. The INCLUDEPICTURE field in the template has a nested MERGEFIELD. This field refers to the EmployeeID field from the database. The GetStream method parses the received URI (the INCLUDEPICTURE field), finds the required data row, and returns the MemoryStream with an image.

Note

Make sure that the MERGEFIELD field nested in the INCLUDEPICTURE field 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;
    }
}
See Also