Image merge byte[]

5 posts, 1 answers
  1. Jose
    Jose avatar
    14 posts
    Member since:
    Sep 2011

    Posted 17 Apr 2012 Link to this post


    My list of data containing an image in byte [].
    How I can include that image in a mail merge.
    I tried using the INCLUDEPICTURE but does not work.

    Is there any way?

    Thanks and sorry for my English,

  2. Answer
    Andrew avatar
    140 posts

    Posted 18 Apr 2012 Link to this post

    Hi Jose,

    RadRichTextBox has an event called StreamFromUriResolving. In addition to inserting INCLUDEPICTURE fields, you should attach to that event which happens every time the current record is changed. The event has StreamFromUriResolvingEventArgs with a property called "Uri" which shows the name of the property being resolved (you can check that property if you have several picture properties in your class).
    When you have identified the property you can set the "Stream" property of the StreamFromUriResolvingEventArgs to a stream you have initialized using the information in the property. For a byte array you can do something like:

    Stream stream = new MemoryStream(buffer);
    e.Stream = stream;
    where buffer is of type byte[].

    You can see how this has been done in the Sivlerlight mail merge demo or the mail merge demo in the Click-once demos.
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Jose
    Jose avatar
    14 posts
    Member since:
    Sep 2011

    Posted 18 Apr 2012 Link to this post

  4. doneill
    doneill avatar
    3 posts
    Member since:
    Sep 2012

    Posted 28 Jan 2013 Link to this post

    Hi.  I am also attempting create a merge document with BarCode images as merge data in SilverLight.  I downloaded a WPF example app from your site and have implemented it in Silverlight.  The obvious issue I am having is that the StreamFromUriResolving event is not firing.  The not so obvious problem is why not ?

    When the source document is created the users select merge fields from a dropdown to be inserted into the document.  I am using the following code:

    // Insert merge fields into document from selection menu
    private void RadContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
        string fieldName = ((RadMenuItem)e.Source).Tag.ToString();
        if (fieldName.ToUpper() == "BARCODE")
            MergeField mf = new MergeField();
            mf.PropertyPath = fieldName;
            IncludePictureField picField = new IncludePictureField();
            picField.SetPropertyValue(IncludePictureField.ImageUriProperty, mf);
            editor.InsertField(new MergeField() { PropertyPath = fieldName });

    Which produces the following merge field: {INCLUDEPICTURE {MERGEFIELD BarCode}}  which looks fine except that the output is always: "Cannot display the image!" so at least it knows it is an image field.

    The rest of the code is as follows:  We call CreateMergeDocument (merge data has already been created and returned from the server).  The source document is opened and the StreamFromURIResolving event is attached.  The barcodes are generated and we do the merge into the output document.

    private RadDocument sourceDocument = null;
    private RadDocument mergedDocument = null;
    private void CreateMergeDocument()
        sourceDocument = OpenRadDocument(template.Attachment);
          sourceDocument.StreamFromUriResolving += sourceDocument_StreamFromUriResolving;

        if (printDocument != null)
            // Generate barcode images
            // Set the datasource
            sourceDocument.MailMergeDataSource.ItemsSource = printMergeData;
            // Perform merge
            mergedDocument = sourceDocument.MailMerge(true);
        printDocument.StreamFromUriResolving -= sourceDocument_StreamFromUriResolving;
    // Generate barcode images into merge data using ZXing library from CodePlex
    private void GenerateBarCodeImages(List<MailMergeData> localizedMergeData)
        var barcodeWriter = new ZXing.BarcodeWriter
            Format = ZXing.BarcodeFormat.CODE_128,
            Options = new ZXing.Common.EncodingOptions
                PureBarcode = true,
                Height = 100,
                Width = 300
        foreach (var mergeRecord in localizedMergeData)
            mergeRecord.BarCodeImage = barcodeWriter.Write(mergeRecord.ParticipantEventBadgeID.ToString());
    void sourceDocument_StreamFromUriResolving(object sender, StreamFromUriResolvingEventArgs e)
        if (e.Uri.ToUpper() == "BARCODE")
            MailMergeData currentMailMessageInfo = (MailMergeData)this.printDocument.MailMergeDataSource.CurrentItem;
            if (currentMailMessageInfo != null)
                // Create stream from WritableBitmap
                byte[] pixels = new byte[4 * currentMailMessageInfo.BarCodeImage.PixelWidth * currentMailMessageInfo.BarCodeImage.PixelHeight];
                System.IO.Stream imageStream = new System.IO.MemoryStream(pixels);
                e.Stream = imageStream;

    You can see that in CreateMergeDocument I am attaching the StreamFromUriResolving event - the code I have provided is a bit modified to simplify it, but I have experimented with attaching that event at different times with the same results.

    The ZXing barcode generator returns a WriteableBitmap image which, based on the WPF example must be converted to a stream which should be fine but never gets executed because the StreamFromUriResolving event never fires.

    The only thing I can think of is that when SetPropertyValue  is set when creating the merge field, it is not using a true URI but a merge field name instead (BarCode) - although it looks likes the WPF app is dong the same thing.

    The other difference between my code an the WPF is that the WPF seems to be doing one-off merges based on drag and drop.  I did modify my code to loop through each merge record and do a separate merge for each, but the event still never fired.

    As always, any help you could offer on this would be greatly appreciated.

  5. Petya
    Petya avatar
    983 posts

    Posted 31 Jan 2013 Link to this post

    Hello doneill,

    Can you please try to subscribe to the StreamFromUriResolving event of RadRichTextBox and execute the respective code there? If this does not solve the issue, please open a support ticket and send us a runnable sample project, as I am having a hard time replicating your setup.

    Let us know how it goes.
    All the best,
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top