Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试将和图像嵌入Word文档时出现关系错误_C#_Openxml_Openxml Sdk_Wordml_Wordprocessingml - Fatal编程技术网

C# 尝试将和图像嵌入Word文档时出现关系错误

C# 尝试将和图像嵌入Word文档时出现关系错误,c#,openxml,openxml-sdk,wordml,wordprocessingml,C#,Openxml,Openxml Sdk,Wordml,Wordprocessingml,我将PNG图像添加到word 2010文档中,如下所示: var imagePart = report.MainDocumentPart.AddImagePart(ImagePartType.Png); var imagePath = Path.Combine(imageFolder, "1.png"); var stream = new FileStream(imagePath, FileMode.Open); imagePart.FeedData(stream); stream.Close(

我将PNG图像添加到word 2010文档中,如下所示:

var imagePart = report.MainDocumentPart.AddImagePart(ImagePartType.Png);
var imagePath = Path.Combine(imageFolder, "1.png");
var stream = new FileStream(imagePath, FileMode.Open);
imagePart.FeedData(stream);
stream.Close();
using(WordprocessingDocument report = WordprocessingDocument.Open(path, true)) {
    //embed the image
}
我找到空图片内容控件的blip元素,并更改其reference属性以指向新图像:

var blip = image.Descendants<Blip>().Single();
blip.Embed = report.MainDocumentPart.GetIdOfPart(imagePart);

什么是关系?为什么不创建一个?如何修复此错误?当我在Word中打开生成的文档时,图像不会显示。

您可以找到一个示例@

我找到了一个解决方案。我不知道为什么,但我不得不

WordprocessingDocument report = WordprocessingDocument.Open(path, true)
使用这样的
语句:

var imagePart = report.MainDocumentPart.AddImagePart(ImagePartType.Png);
var imagePath = Path.Combine(imageFolder, "1.png");
var stream = new FileStream(imagePath, FileMode.Open);
imagePart.FeedData(stream);
stream.Close();
using(WordprocessingDocument report = WordprocessingDocument.Open(path, true)) {
    //embed the image
}

没有
使用
文档保存不正确:没有创建关系。

这篇文章介绍了如何添加一个全新的图像,不是如何在现有图片内容控件中引用它对于图片内容控件,您可以阅读在打开的XML WordprocessingML文档中替换图片内容控件中的图片@我尝试了这个,但没有为我解决问题。还有其他人找到其他修复方法吗?