Back to Devexpress

IUriStreamProvider.GetStream(String) Method

officefileapi-devexpress-dot-office-dot-services-dot-iuristreamprovider-dot-getstream-x28-system-dot-string-x29.md

latest5.6 KB
Original Source

IUriStreamProvider.GetStream(String) Method

Provides the data stream for the specified Uri.

Namespace : DevExpress.Office.Services

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

NuGet Package : DevExpress.Office.Core

Declaration

csharp
Stream GetStream(
    string uri
)
vb
Function GetStream(
    uri As String
) As Stream

Parameters

NameTypeDescription
uriString

A string specifying the URI of the object that is the source of the stream.

|

Returns

TypeDescription
Stream

A Stream object specifying a data stream or null ( Nothing in Visual Basic).

|

Remarks

If you register a custom Uri stream provider using the IUriStreamService.RegisterProvider method, its GetStream method is called when it is necessary to get data for the URI encountered in the document. It can provide data for external images represented by their URI or for the calculated fields.

For instance, you can use a custom URI provider to process a custom switch of the INCLUDEPICTURE field. Implement a GetStream method, so it retrieves images from a custom source specified by the value of a custom switch. This approach is illustrated in the ‘Merge Database Records’ demo shipped with installation.

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

See Also

IUriStreamProvider Interface

IUriStreamProvider Members

DevExpress.Office.Services Namespace