You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OpenXML向Docx插入Bitmap后文档无法打开的技术求助

向Docx插入Bitmap图片后Word无法打开文档

向Docx文件添加Bitmap图片后,用Microsoft Word打开时出现以下错误:

Word experienced an error trying to open the file.

我的代码如下:

using (WordprocessingDocument doc = WordprocessingDocument.Open(saveAsPath, true))
{
    Bitmap bm = GeneratCadImage();
    
    string fileName = @"C:\test.bmp";
    var codeBitmap = new Bitmap(fileName);
    Image image = (Image)codeBitmap;    
    Drawing drawing = ConvertBitmapToDrawing(doc, codeBitmap);
    Run newRun = new Run(drawing);

    //我试过多个插入位置
    OpenXmlElement contentControl = doc.MainDocumentPart.Document.Body.Descendants().Where(x => x != null).Last() ;

    contentControl.InsertAfterSelf(newRun);    
}

关于插入图片的位置,我试过第一个、最后一个等位置,排除位置因素的影响。

一个关键线索:图片本身大小约7Mb,但插入后文档大小仅增加了10kb。

以下是ConvertBitmapToDrawing方法(代码大多复制自MSDN文档):

static Drawing ConvertBitmapToDrawing(WordprocessingDocument wordDoc, System.Drawing.Bitmap image)
{
    MainDocumentPart mainDocumentPart = wordDoc.MainDocumentPart;
    ImagePart imagePart = mainDocumentPart.AddImagePart(ImagePartType.Bmp);
    using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
    {
        image.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        stream.Position = 0;
        imagePart.FeedData(stream);
    }

    string imagePartId = mainDocumentPart.GetIdOfPart(imagePart);

    var element =
             new Drawing(
                 new DW.Inline(
                     new DW.Extent() { Cx = 990000L, Cy = 792000L },
                     new DW.EffectExtent()
                     {
                         LeftEdge = 0L,
                         TopEdge = 0L,
                         RightEdge = 0L,
                         BottomEdge = 0L
                     },
                     new DW.DocProperties()
                     {
                         Id = (UInt32Value)1U,
                         Name = "Picture 1"
                     },
                     new DW.NonVisualGraphicFrameDrawingProperties(
                         new A.GraphicFrameLocks() { NoChangeAspect = true }),
                     new A.Graphic(
                         new A.GraphicData(
                             new PIC.Picture(
                                 new PIC.NonVisualPictureProperties(
                                     new PIC.NonVisualDrawingProperties()
                                     {
                                         Id = (UInt32Value)0U,
                                         Name = "New Bitmap Image.jpg"
                                     },
                                     new PIC.NonVisualPictureDrawingProperties()),
                                 new PIC.BlipFill(
                                     new A.Blip(
                                         new A.BlipExtensionList(
                                             new A.BlipExtension()
                                             {
                                                 Uri =
                                                    "{28A0092B-C50C-407E-A947-70E740481C1C}"
                                             })
                                     )
                                     {
                                         Embed = imagePartId,
                                         CompressionState =
                                         A.BlipCompressionValues.Print
                                     },
                                     new A.Stretch(
                                         new A.FillRectangle())),
                                 new PIC.ShapeProperties(
                                     new A.Transform2D(
                                         new A.Offset() { X = 0L, Y = 0L },
                                         new A.Extents() { Cx = 990000L, Cy = 792000L }),
                                     new A.PresetGeometry(
                                         new A.AdjustValueList()
                                     )
                                     { Preset = A.ShapeTypeValues.Rectangle }))
                         )
                         { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                 )
                 {
                     DistanceFromTop = (UInt32Value)0U,
                     DistanceFromBottom = (UInt32Value)0U,
                     DistanceFromLeft = (UInt32Value)0U,
                     DistanceFromRight = (UInt32Value)0U,
                     EditId = "50D07946"
                 });

    return element;
}

编辑补充:

@pfx:我无法直接运行你的代码,出现以下错误:

Cannot insert the OpenXmlElement "newChild" because it is part of a tree

不过我修改成以下代码后可以运行:

Drawing drawing = ConvertBitmapToDrawing(doc, codeBitmap);
Run newRun = new Run(drawing);
Paragraph para = new();
para.Append(newRun);
doc.MainDocumentPart.Document.Body.AppendChild(para);

但修改后仍然出现同样的问题:Word无法打开文档。

内容的提问来源于stack exchange,提问作者Richard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 23:45:40