VictorB
VictorB

Reputation: 411

insert into picture content control with Open XML

How do I insert a picture (located somewhere on my HDD) in a Picture Content Control using Open XML SDK? This should be an easy task, but surprisingly I couldn't find any examples or answers on the web.

Any help would be welcomed.

Thank you,

Victor

Upvotes: 0

Views: 2498

Answers (3)

J. Horn
J. Horn

Reputation: 81

I had the same issue and same frustration at the lack of examples, but was able to find something that worked for me. Basically, I stream an image into a byte[], save that stream as a .png, and take that byte[].ToArray() in a XMLWriter.WriteBase64 element that is bound to an image content control.

var pic = Image.FromFile(server.MapPath(@"images/someImage.png"));
var picStream = new MemoryStream {Position = 0};
pic.Save(picStream, System.Drawing.Imaging.ImageFormat.Png);
var picBytes = picStream.ToArray();

writer.WriteStartElement("RiskScoreImage");
writer.WriteBase64(picBytes, 0, picBytes.Length);
writer.WriteEndElement();

picStream.Flush();
picStream.Close();
pic.Dispose();

Upvotes: 3

Andrew
Andrew

Reputation: 5277

Although I can't give you a definitive answer (I haven't done anything with openXML for about a year), I'd suggest having a browse through the source code of the DocX project on codeplex. From what I remember they have support for pictures.

Upvotes: -1

Related Questions