Using IWPicture for MailMerge

Hi

Is there a possibility to use IWPicture object to insert image in the word document that's using merge fields? Currently when merging picture I am using following code:

private void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs args)
        {
            if (args.FieldValue != null)
            {
                var stream = new System.IO.MemoryStream();
                Image image = (Image)args.FieldValue;
                ImageFormat imageFormat = image.RawFormat;
                image = new Bitmap(image, GetNewSize(args.CurrentMergeField.FieldCode, image.Size));
                image.Save(stream, imageFormat);
                stream.Position = 0;
                args.ImageStream = stream;
            }
        }

In the method GetNewSize, I am calculating size of the image to fit in certain dimensions. This code works, however picture quality is lost. I noticed however when I use your IWPicture that by resizing picture quality isn't lost. For example if I use following code, to add picture directly in the document (in text box) picture looks much nicer:

Private Sub FillTextboxWithUserImage(textBox As WTextBox, paragraph As IWParagraph)
            Dim profileImage = GetProfileImage(candidateID)
            Dim backup As Image = New Bitmap(profileImage)
            paragraph.Text = ""
            textBox.TextBoxFormat.FillEfects.Type = BackgroundType.NoBackground
            Dim picture As IWPicture = paragraph.AppendPicture(backup)
            Dim scalingFactor As Single = Math.Min(textBox.TextBoxFormat.Height / picture.Height, textBox.TextBoxFormat.Width / picture.Width) * 100
            picture.WidthScale = scalingFactor
            picture.HeightScale = scalingFactor
        End Sub

If I use this method, then picture quality is somehow preserved even though it is correctly resized.

 

1 Reply 1 reply marked as answer

HC Hemalatha Chiranjeevulu Syncfusion Team April 9, 2021 05:48 PM UTC

Hi Sasa,

Thank you for contacting Syncfusion support.

From the details, we have found that your requirement is not to loss image quality after resizing the image. To achieve your requirement, we suggest to you to use the code snippets from the below link
https://github.com/SyncfusionExamples/Mail-Merge-Examples/blob/master/Fit-photo-within-textbox/Console-App-.NET-Framework/Fit-photo-within-textbox/Program.cs

Please let us know if you have any other questions.

Regards,
Hemalatha C



Marked as answer
Loader.
Up arrow icon