This is a migrated thread and some comments may be shown as answers.

Image merge byte[]

4 Answers 184 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Jose
Top achievements
Rank 1
Jose asked on 17 Apr 2012, 10:17 AM
Hello,

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,

Jose

4 Answers, 1 is accepted

Sort by
0
Accepted
Andrew
Telerik team
answered on 18 Apr 2012, 10:58 AM
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.
Greetings,
Andrew
the Telerik team

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

0
Jose
Top achievements
Rank 1
answered on 18 Apr 2012, 12:12 PM
Thanks!!
0
doneill
Top achievements
Rank 1
answered on 29 Jan 2013, 12:17 AM
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);
        this.editor.InsertField(picField);
    }
    else
    {
        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
        GenerateBarCodeImages(printMergeData);
 
        // 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.
Thanks.

0
Petya
Telerik team
answered on 31 Jan 2013, 11:34 AM
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,
Petya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Derek
Top achievements
Rank 1
commented on 20 Jan 2023, 02:01 AM

I'm curious if this was ever resolved? I have the exact same question as Doneill, and my code is very similar. I basically have a sourceDocument (RadDocument) to which I subscribe to a StreamFromUriResolving handler, but the handler never fires. I don't understand the previous answer about subscribing to the RadRichTextBox.StreamFromUriResolving event. Which RadRichTextBox would that be? I don't have a reference to any of those anywhere.  I'm working with a RadDocument.

Thanks.

Yoan
Telerik team
commented on 24 Jan 2023, 02:54 PM

Hello, Derek

I tested the code provided by Doneill and it happens to work as expected on my side. I incorporated that logic in simple a project which uses just RadDocument, without the RadRichTextBox in order to best fit your case. I have attached that project for you to examine and modify as you like.

If you happen to need any further and more specific information, please submit a new ticket that is specific to your case with a bit more details and an attached sample project if possible. That way we can inspect it and get back to you with specifics.

Looking forward to hearing from you.

Regards,

Yoan

Tags
RichTextBox
Asked by
Jose
Top achievements
Rank 1
Answers by
Andrew
Telerik team
Jose
Top achievements
Rank 1
doneill
Top achievements
Rank 1
Petya
Telerik team
Share this question
or