Mail Merge Image field

Hello there,

My template document for mail merge has IMAGE fields.

In most of the cases Images will have a value of byte[] in datasource but in some cases the mail merge data source is not having the required value,

so value of that merge field will have something like:
collection[IMAGE:IMAGEPPHOTOMAIN] = ## FIELD: IMAGE:IMAGEPPHOTOMAIN DOES NOT EXIST##
Now, in MailMerge_MergeImageField() handler, I am doing something like

private static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs e)
{
    byte[] imageBytes = (e.FieldValue as byte[]);

    if (imageBytes != null)
    {
        e.Image = System.Drawing.Image.FromStream(new MemoryStream((byte[]) e.FieldValue));
    }
    else
    {
        e.Image = null;
    }
}
}

but running this throws the error: (for else case, while I am setting image to null)

Cannot load image from field ‘ImagePPhotoMain’. The field contains data in unsupported format. Invalid URI: The URI scheme is not valid.

Please guide how can I avoid replacing the merge field in this case.

Thank you!

Hi

Thanks for your inquiry. Maybe you should just remove field in such case. Please see the following code:

private static void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs e)
{
    byte[] imageBytes = (e.FieldValue as byte[]);
    if (imageBytes != null)
        e.Image = Image.FromStream(new MemoryStream((byte[]) e.FieldValue));
    else
        e.Field.Remove();
}

Hope this helps.
Best regards.

Thank you Alexey!