Skip to main content

IUriStreamProvider.GetStream(String) Method

Provides the data stream for the specified Uri.

Namespace: DevExpress.Office.Services

Assembly: DevExpress.Office.v24.1.Core.dll

NuGet Packages: DevExpress.Office.Core, DevExpress.Win.Navigation

Declaration

Stream GetStream(
    string uri
)

Parameters

Name Type Description
uri String

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

Returns

Type Description
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

This example demonstrates how to implement the IUriStreamProvider interface to create a custom data stream provider for the IUriProviderService service.

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;
    }
See Also