尝试在C#中将图像添加到Word文档时,我收到了无效的文件名异常。
图像会保存但使用相同的文件名保存到该位置但无法嵌入到文档中。
foreach (XmlElement childnodes in node.GetElementsByTagName("events"))
{
string name = childnodes.GetElementsByTagName("name")[0].InnerText;
string location = childnodes.GetElementsByTagName("location")[0].InnerText;
string time = childnodes.GetElementsByTagName("time")[0].InnerText;
XmlAttributeCollection xmlAttributeCollection = childnodes.GetElementsByTagName("image")[0].Attributes;
if (xmlAttributeCollection != null)
{
var newName = FilenameFromTitle(name);
var image = xmlAttributeCollection["src"].InnerText;
var imagename = "images/" + newName + ".jpg";
using (WebClient client = new WebClient())
{
client.DownloadFile(image,newName);
}
document.Content.Text += "===================================================================================";
document.Content.InlineShapes.AddPicture(imagename,false,true);
document.Content.Text += "EventName" + name;
document.Content.Text += "Location" + location;
document.Content.Text += "Time" + time;
}
}
这是其中一个生成名称的示例。
"影像/ CORB-隆德-CA-altcountry - 萨拉简-scouten-CA-am.jpg"
提前致谢