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 开发者_Go百科surprisingly I couldn't find any examples or answers on the web.
Any help would be welcomed.
Thank you,
Victor
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();
take a look at this: http://ericwhite.com/blog/2011/03/27/replacing-a-picture-in-a-picture-content-control-in-an-open-xml-wordprocessingml-document/
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.
精彩评论