Back to Devexpress

IUriStreamService.RegisterProvider(IUriStreamProvider) Method

officefileapi-devexpress-dot-office-dot-services-dot-iuristreamservice-dot-registerprovider-x28-devexpress-dot-office-dot-services-dot-iuristreamprovider-x29.md

latest10.1 KB
Original Source

IUriStreamService.RegisterProvider(IUriStreamProvider) Method

Registers the Uri data stream provider and makes it available to clients of the service.

Namespace : DevExpress.Office.Services

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

NuGet Package : DevExpress.Office.Core

Declaration

csharp
void RegisterProvider(
    IUriStreamProvider provider
)
vb
Sub RegisterProvider(
    provider As IUriStreamProvider
)

Parameters

NameTypeDescription
providerIUriStreamProvider

An object which exposes the IUriStreamProvider interface.

|

Remarks

Use the RegisterProvider method to add a custom provider object that is used by a IUriStreamService service to retrieve data from the specified URI.

To obtain the data stream from the URI encountered in the document (such as an inline image or a calculated field with the corresponding switch) the RichEditControl accesses the IUriStreamService. The service processes the URI with the help of registered URI stream providers.

This implementation enables you to create and register a custom URI Stream provider that is capable of processing a custom URI in a specific way, for example, to retrieve images for the INCLUDEPICTURE field from a database.

The following code snippet uses the IUriStreamService.RegisterProvider method to register a custom data stream provider implementing the IUriStreamProvider interface and make it available to clients of the IUriStreamService service.

View Example

csharp
private void RegisterUriStreamService(RichEditControl richEditControl) {
    IUriStreamService uriStreamService = richEditControl.GetService<IUriStreamService>();
    uriStreamService.RegisterProvider(new ImageStreamProvider(NorthwindDataProvider.Categories, "Picture"));
}

View Example: Use Images When Implementing Mail Merge

The following code snippet implements the IUriStreamProvider interface to create a custom data stream provider for the IUriProviderService service.

csharp
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) {
        uri = uri.Trim();
        if (!uri.StartsWith(prefix))
            return null;
        string strId = uri.Substring(prefix.Length).Trim();
        int id;
        if (!int.TryParse(strId, out id))
            return null;
        DataRow row = table.Rows.Find(id);
        if (row == null)
            return null;
        byte[] bytes = row[columnName] as byte[];
        if (bytes == null)
            return null;

        // Use this approach to trim the OLE header off the image
        // See also: https://supportcenter.devexpress.com/ticket/details/q233460/how-do-i-bind-a-pictureedit-to-sql-server-image-column, 
        // https://social.msdn.microsoft.com/Forums/en-US/c37289c7-3ca5-458e-8eda-286ffa2ff966/retrieving-an-image-from-a-table-in-a-c-picturebox?forum=sqldataaccess
        MemoryStream memoryStream = new MemoryStream();
        int oleHeaderOffset = 78;
        memoryStream.Write(bytes, oleHeaderOffset, bytes.Length - oleHeaderOffset);

        return memoryStream;
    }
vb
Public Class ImageStreamProvider
    Implements IUriStreamProvider
    Private 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 Implements IUriStreamProvider.GetStream
        uri = uri.Trim()
        If (Not uri.StartsWith(prefix)) Then
            Return Nothing
        End If
        Dim strId As String = uri.Substring(prefix.Length).Trim()
        Dim id As Integer
        If (Not Integer.TryParse(strId, id)) Then
            Return Nothing
        End If
        Dim row As DataRow = table.Rows.Find(id)
        If row Is Nothing Then
            Return Nothing
        End If
        Dim bytes() As Byte = TryCast(row(columnName), Byte())
        If bytes Is Nothing Then
            Return Nothing
        End If

        ' Use this approach to trim the OLE header off the image
        ' See also: https://supportcenter.devexpress.com/ticket/details/q233460/how-do-i-bind-a-pictureedit-to-sql-server-image-column, 
        ' https://social.msdn.microsoft.com/Forums/en-US/c37289c7-3ca5-458e-8eda-286ffa2ff966/retrieving-an-image-from-a-table-in-a-c-picturebox?forum=sqldataaccess
        Dim memoryStream As New MemoryStream()
        Dim oleHeaderOffset As Integer = 78
        memoryStream.Write(bytes, oleHeaderOffset, bytes.Length - oleHeaderOffset)

        Return memoryStream
    End Function

End Class

The following code snippets (auto-collected from DevExpress Examples) contain references to the RegisterProvider(IUriStreamProvider) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

how-to-import-html-files-that-contain-images-referenced-with-custom-prefix/CS/Program.cs#L23

csharp
IUriStreamService uriStreamService = wordProcessor.GetService<IUriStreamService>();
uriStreamService.RegisterProvider(new CustomUriStreamProvider(basePath, "bmp"));
wordProcessor.LoadDocument(basePath + "test.html");

winforms-richedit-use-images-in-mail-merge/CS/Form1.cs#L31

csharp
IUriStreamService uriStreamService = richEditControl.GetService<IUriStreamService>();
    uriStreamService.RegisterProvider(new ImageStreamProvider(NorthwindDataProvider.Categories, "Picture"));
}

word-processing-use-images-in-richedit-mail-merge/CS/Program.cs#L30

csharp
IUriStreamService uriStreamService = richEditDocumentServer.GetService<IUriStreamService>();
    uriStreamService.RegisterProvider(new ImageStreamProvider(NorthwindDataProvider.Categories, "Picture"));
}

office-file-api-in-web-api-app/CS/Controllers/RichEditController.cs#L157

csharp
IUriStreamService uriService = (IUriStreamService)wordProcessor.GetService(typeof(IUriStreamService));
uriService.RegisterProvider(new MailMergeUriStreamProvider());
await wordProcessor.LoadDocumentAsync("Documents/MailMerge.docx");

how-to-import-html-files-that-contain-images-referenced-with-custom-prefix/VB/Program.vb#L23

vb
Dim uriStreamService As IUriStreamService = wordProcessor.GetService(Of IUriStreamService)()
uriStreamService.RegisterProvider(New CustomUriStreamProvider(basePath, "bmp"))
wordProcessor.LoadDocument(basePath & "test.html")

winforms-richedit-use-images-in-mail-merge/VB/Form1.vb#L27

vb
Dim uriStreamService As IUriStreamService = richEditControl.GetService(Of IUriStreamService)()
    uriStreamService.RegisterProvider(New ImageStreamProvider(NorthwindDataProvider.Categories, "Picture"))
End Sub

word-processing-use-images-in-richedit-mail-merge/VB/Program.vb#L25

vb
Dim uriStreamService As IUriStreamService = richEditDocumentServer.GetService(Of IUriStreamService)()
    uriStreamService.RegisterProvider(New ImageStreamProvider(NorthwindDataProvider.Categories, "Picture"))
End Sub

See Also

IUriStreamService Interface

IUriStreamService Members

DevExpress.Office.Services Namespace