WPF Canvas 使用 OpenXML 到 word 文档

WPF Canvas to word documents using OpenXML

我目前正在开展一个项目,该项目需要将在 WPF Canvas 上绘制的图像(主要是路径中的直线和椭圆等几何图形)导入 Word 文档。我正在使用 Openxml 来完成这项工作。这是我所做的

这是我目前所知道的,

Canvas通过下面一段代码转成内存流

public static Stream CreateJPGStream(this Canvas canvas)
    {
        RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)canvas.ActualWidth, (int)canvas.ActualHeight, 96d, 96d, PixelFormats.Pbgra32);
        canvas.Measure(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight));
        canvas.Arrange(new Rect(new Size((int)canvas.ActualWidth, (int)canvas.ActualHeight)));
        renderBitmap.Render(canvas);
        var stream = new MemoryStream();
        var encoder = new JpgBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
        encoder.Save(stream);
        return stream;
    }

我相信这不是问题的原因,因为我已经使用这段代码一段时间了,到目前为止我还没有遇到任何问题

然后我刚刚修改了 Microsoft Guide

的代码
    public static void InsertPicture(this WordprocessingDocument word, Stream stream)
    {
        MainDocumentPart mainPart = word.MainDocumentPart;
        if (mainPart == null)
        {
            mainPart = word.AddMainDocumentPart();
            mainPart.Document = new Document() { Body = new Body() };
        }
        ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Jpeg);
        imagePart.FeedData(stream);
        AddImageToBody(word, mainPart.GetIdOfPart(imagePart));
    }

        private static void AddImageToBody(WordprocessingDocument wordDoc, string relationshipId)
    {
        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 = 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 = relationshipId,
                                         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 = 0U,
                     DistanceFromBottom = 0U,
                     DistanceFromLeft = 0U,
                     DistanceFromRight = 0U,
                     EditId = "50D07946"
                 });
        wordDoc.MainDocumentPart.Document.Body.AppendChild(new Paragraph(new Run(element)));
    }

现在将这两种方法结合起来

    public void InsertPicture(Canvas c)
    {
          using (var word = OpenXmlHelper.Create(filePath))
                {
                var stream = c.CreateJPGStream();
                word.InsertPicture(stream);
                 }
   }

但是当我打开文档时,我得到了这个

我是不是在 memorysteam 或 openxml 方面做错了什么?有哪位大神能赐教吗

PS:我已经看过类似的问题 Inserting Image into DocX using OpenXML and setting the size

写入流后,流的位置将在流的末尾。在阅读流之前,您必须将流倒回到开头。

stream.Position = 0;

stream.Seek(0, System.IO.SeekOrigin.Begin);