Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert image from byte array into OpenXML CustomXmlBlock

I'm working with a template document that uses CustomXmlBlocks as placeholders to place tables and other information. I need to be able to place an image into one of these blocks somehow... even if it is placed into a run first.

The images come back from the database in a dataset as a byte array (.bmp format ultimately).

I was trying to do something like this just to see if I could even get the image to show up in the document but to no avail:

ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Bmp);
MemoryStream imageStream = new MemoryStream(imgData); //imgData is the byte array

imagePart.FeedData(imageStream);

I keep thinking there has to be an easy way to just take the byte stream and place it into the document but I haven't been able to find any examples anywhere. Do I now need to do something else with that imagePart?

like image 685
Sunspot Avatar asked Nov 13 '22 19:11

Sunspot


1 Answers

Yes, you do need to do something else with the imagePart; you need to add an image structure (either an old VML w:pict or a graphic) to your main document part which refers to it. See How to: Insert a Picture into a Word Processing Document

Alternatively, you can add it to a CustomXML part, and suck it in using a picture content control.

like image 141
JasonPlutext Avatar answered Dec 10 '22 12:12

JasonPlutext