Back to Devexpress

IUriStreamProvider Interface

officefileapi-devexpress-dot-office-dot-services.md

latest4.9 KB
Original Source

IUriStreamProvider Interface

Allows you to retrieve data from a URI.

Namespace : DevExpress.Office.Services

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

NuGet Package : DevExpress.Office.Core

Declaration

csharp
[ComVisible(true)]
public interface IUriStreamProvider
vb
<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.

csharp
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;
    }
}
vb
Imports DevExpress.Office.Services
Imports System
Imports System.Data
Imports System.IO

Public Class ImageStreamProvider
    Inherits IUriStreamProvider

    Shared ReadOnly prefix As String = "dbimg://"
    Private table As DataTable
    Private columnName As String

    Public Sub New(ByVal sourceTable As DataTable, ByVal imageColumn As String)
        Me.table = sourceTable
        Me.columnName = imageColumn
    End Sub

    Public Function GetStream(ByVal uri As String) As Stream
        uri = uri.Trim()
        If Not uri.StartsWith(prefix) Then Return Nothing
        Dim strId As String = uri.Substring(prefix.Length).Trim()
        Dim id As Integer
        If Not Integer.TryParse(strId, id) Then Return Nothing
        Dim row As DataRow = table.Rows.Find(id)
        If row Is Nothing Then Return Nothing
        Dim bytes As Byte() = TryCast(Convert.FromBase64String(TryCast(row(columnName), String)), Byte())
        If bytes Is Nothing Then Return Nothing
        Dim memoryStream As MemoryStream = New MemoryStream(bytes)
        Return memoryStream
    End Function
End Class

See Also

IUriStreamProvider Members

INCLUDEPICTURE

DevExpress.Office.Services Namespace