Free Support Forum - aspose.com

Insert Image into MSWord from a BLOB

I would like to insert images saved in SQL as BLOBs into Word using VB.NET. Is this possible without first saving to the file system? If so, how would I go about doing this.

Hi,

Yes, you don’t have to save images to file first. You need to:

{
//Open the data reader. It needs to be in the normal mode that reads all record at once.
dataReader = cmd.ExecuteReader();

//Set up the event handler for image fields and perform mail merge.
doc.MailMerge.MergeImageField += new MergeImageFieldEventHandler(HandleMergeEmployeePhoto);
doc.MailMerge.ExecuteWithRegions(dataReader, “Employees”);
}

//Define the event handler
private void HandleMergeEmployeePhoto(object sender, MergeImageFieldEventArgs e)
{
//e.FieldName will contain name of the field. Will be “MyImage” if you had “Image:MyImage” field in the document.
//e.FieldValue will contain value that comes from your data source. This is an object and in your case it will be the byte array containing data of the BLOB field.
MemoryStream imageStream = new MemoryStream((byte[])e.FieldValue);
//Now the mail merge engine will retrieve the image from the stream.
e.ImageStream = imageStream;
}

By the way I’m building a fully working sample for this scenario. It will be available in product demos and online soon.

The demo for this feature is now available online and in the latest Aspose.Word 1.3.1.

Thanks. That does help.