2

我正在尝试从 docx 文件中导出图像。如何将来自 Shape 对象的 EncodedPackage 转换为图像?

我的代码示例:

DocumentFormat.OpenXml.Vml.Shape shape = imageElement.Descendants<DocumentFormat.OpenXml.Vml.Shape>().FirstOrDefault();
byte[] bytes = System.Convert.FromBase64String(shape.EncodedPackage.Value.Replace("\n", ""));
System.Drawing.Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
   image = System.Drawing.Image.FromStream(ms);
}
image.Save(fileName);
4

1 回答 1

0

您可以从中获取图像ImagePart。尝试这个:

var imageParts = doc.MainDocumentPart.ImageParts;
foreach (var imagePart in imageParts)
{
      var uri = imagePart.Uri;
      var filename = uri.ToString().Split('/').Last();
      var stream = doc.Package.GetPart(uri).GetStream();

       Bitmap b = new Bitmap(stream);
       b.Save(@"C:\Extracted_Images\" + filename);
}
于 2013-09-06T08:53:39.387 回答