Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.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# 打开XML将图片添加到演示文稿_C#_Powerpoint_Openxml - Fatal编程技术网

C# 打开XML将图片添加到演示文稿

C# 打开XML将图片添加到演示文稿,c#,powerpoint,openxml,C#,Powerpoint,Openxml,我正在尝试动态创建power point演示文稿(不使用先前创建的模板幻灯片)。到目前为止,我有结果,但在添加图片时遇到困难(请参阅下面的代码) 禁用p.Picture部件时,它正在工作(意味着可以打开创建的pptx文件) 对于p.图片部分,PowerPoint建议修复,并删除“发现的不可读部分”,从而只显示带有“图片无法显示消息”的图片占位符。如果您在未修复状态下展开pptx,则媒体文件夹中包含可以打开的正确图片_rels文件夹包含正确链接现有图像文件和图像占位符的文件。。。我肯定错过了什么,

我正在尝试动态创建power point演示文稿(不使用先前创建的模板幻灯片)。到目前为止,我有结果,但在添加图片时遇到困难(请参阅下面的代码)

禁用p.Picture部件时,它正在工作(意味着可以打开创建的pptx文件)

对于p.图片部分,PowerPoint建议修复,并删除“发现的不可读部分”,从而只显示带有“图片无法显示消息”的图片占位符。如果您在未修复状态下展开pptx,则媒体文件夹中包含可以打开的正确图片_rels文件夹包含正确链接现有图像文件和图像占位符的文件。。。我肯定错过了什么,但是什么

感谢您的帮助!非常感谢

    public static void CreatePresentation(string filepath)
    {
        // Create a presentation at a specified file path. The presentation document type is pptx, by default.
        PresentationDocument presentationDoc = PresentationDocument.Create(filepath, PresentationDocumentType.Presentation);
        PresentationPart presentationPart = presentationDoc.AddPresentationPart();
        presentationPart.Presentation = new Presentation();

        SlideMasterIdList sldMasterIdList = new SlideMasterIdList(new SlideMasterId() { Id = (UInt32Value)2147483648U, RelationshipId = "rId1" });
        SlideIdList sldIdList = new SlideIdList();
        SlideSize sldSize = new SlideSize() { Cx = 2751 * mm / 10, Cy = 1905 * mm / 10, Type = SlideSizeValues.Screen4x3 }; // DIN A4 - Landscape.
        NotesSize notesSize = new NotesSize() { Cx = 1905 * mm / 10, Cy = 2751 * mm / 10 }; // DIN A4 - Portrait.
        DefaultTextStyle defaultTextStyle = new DefaultTextStyle();

        presentationPart.Presentation.Append(sldMasterIdList, sldIdList, sldSize, notesSize, defaultTextStyle);

        #region [ SlidePart ]
        SlidePart slidePart = presentationPart.AddNewPart<SlidePart>("rId2");
        sldIdList.AppendChild<SlideId>(new SlideId() { Id = (UInt32Value)256U, RelationshipId = "rId2" });

        slidePart.Slide = new Slide
        (
            new CommonSlideData
            (
                new ShapeTree
                (
                    new P.NonVisualGroupShapeProperties
                    (
                        new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
                        new P.NonVisualGroupShapeDrawingProperties(),
                        new ApplicationNonVisualDrawingProperties()
                    ),
                    new GroupShapeProperties(new DocumentFormat.OpenXml.Drawing.TransformGroup())
                )
            ),
            new ColorMapOverride(new MasterColorMapping())
        );

        ImagePart ip = slidePart.AddImagePart(ImagePartType.Jpeg, "rId2");
        MyStream.Position = 0;
        ip.FeedData(MyStream);
        #endregion

        #region [ slideLayoutPart ]
        SlideLayoutPart slideLayoutPart = slidePart.AddNewPart<SlideLayoutPart>("rId1");
        SlideLayout slideLayout = new SlideLayout
            (
                new CommonSlideData
                    (
                        new ShapeTree
                            (
                                new P.NonVisualGroupShapeProperties
                                    (
                                        new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
                                        new P.NonVisualGroupShapeDrawingProperties(),
                                        new ApplicationNonVisualDrawingProperties()
                                    ),
                                    new GroupShapeProperties
                                        (
                                            new DocumentFormat.OpenXml.Drawing.TransformGroup()
                                        )
                            )
                    ),
                    new ColorMapOverride
                        (
                            new MasterColorMapping()
                        )
            );
        slideLayoutPart.SlideLayout = slideLayout;
        #endregion

        #region [ slideMasterPart ]
        SlideMasterPart slideMasterPart = slideLayoutPart.AddNewPart<SlideMasterPart>("rId1");
        SlideMaster slideMaster = new SlideMaster(
        new CommonSlideData(new ShapeTree(
          new P.NonVisualGroupShapeProperties(
          new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
          new P.NonVisualGroupShapeDrawingProperties(),
          new ApplicationNonVisualDrawingProperties()),
          new GroupShapeProperties(new DocumentFormat.OpenXml.Drawing.TransformGroup()),
          new P.Shape(
          new P.NonVisualShapeProperties(
            new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "Title Placeholder 1" },
            new P.NonVisualShapeDrawingProperties(new ShapeLocks() { NoGrouping = true }),
            new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title })),
          new P.ShapeProperties(),
          new P.TextBody(
            new BodyProperties(),
            new ListStyle(),
            new Paragraph())))),
        new P.ColorMap() { Background1 = D.ColorSchemeIndexValues.Light1, Text1 = D.ColorSchemeIndexValues.Dark1, Background2 = D.ColorSchemeIndexValues.Light2, Text2 = D.ColorSchemeIndexValues.Dark2, Accent1 = D.ColorSchemeIndexValues.Accent1, Accent2 = D.ColorSchemeIndexValues.Accent2, Accent3 = D.ColorSchemeIndexValues.Accent3, Accent4 = D.ColorSchemeIndexValues.Accent4, Accent5 = D.ColorSchemeIndexValues.Accent5, Accent6 = D.ColorSchemeIndexValues.Accent6, Hyperlink = D.ColorSchemeIndexValues.Hyperlink, FollowedHyperlink = D.ColorSchemeIndexValues.FollowedHyperlink },
        new SlideLayoutIdList(new SlideLayoutId() { Id = (UInt32Value)2147483649U, RelationshipId = "rId1" }),
        new TextStyles(new TitleStyle(), new BodyStyle(), new OtherStyle()));
        slideMasterPart.SlideMaster = slideMaster;
        slideMasterPart.AddPart(slideLayoutPart, "rId1");
        presentationPart.AddPart(slideMasterPart, "rId1");
        #endregion

        #region [ Theme ]
        ThemePart themePart = slideMasterPart.AddNewPart<ThemePart>("rId5");
        D.Theme theme = new D.Theme() { Name = "Office Theme" };

        D.ThemeElements themeElements = new D.ThemeElements(
        new D.ColorScheme(
          new D.Dark1Color(new D.SystemColor() { Val = D.SystemColorValues.WindowText, LastColor = "000000" }),
          new D.Light1Color(new D.SystemColor() { Val = D.SystemColorValues.Window, LastColor = "FFFFFF" }),
          new D.Dark2Color(new D.RgbColorModelHex() { Val = "1F497D" }),
          new D.Light2Color(new D.RgbColorModelHex() { Val = "EEECE1" }),
          new D.Accent1Color(new D.RgbColorModelHex() { Val = "4F81BD" }),
          new D.Accent2Color(new D.RgbColorModelHex() { Val = "C0504D" }),
          new D.Accent3Color(new D.RgbColorModelHex() { Val = "9BBB59" }),
          new D.Accent4Color(new D.RgbColorModelHex() { Val = "8064A2" }),
          new D.Accent5Color(new D.RgbColorModelHex() { Val = "4BACC6" }),
          new D.Accent6Color(new D.RgbColorModelHex() { Val = "F79646" }),
          new D.Hyperlink(new D.RgbColorModelHex() { Val = "0000FF" }),
          new D.FollowedHyperlinkColor(new D.RgbColorModelHex() { Val = "800080" })) { Name = "Office" },
          new D.FontScheme(
          new D.MajorFont(
          new D.LatinFont() { Typeface = "Calibri" },
          new D.EastAsianFont() { Typeface = "" },
          new D.ComplexScriptFont() { Typeface = "" }),
          new D.MinorFont(
          new D.LatinFont() { Typeface = "Calibri" },
          new D.EastAsianFont() { Typeface = "" },
          new D.ComplexScriptFont() { Typeface = "" })) { Name = "Office" },
          new D.FormatScheme(
          new D.FillStyleList(
          new D.SolidFill(new D.SchemeColor() { Val = D.SchemeColorValues.PhColor }),
          new D.GradientFill(
            new D.GradientStopList(
            new D.GradientStop(new D.SchemeColor(new D.Tint() { Val = 50000 },
              new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 },
            new D.GradientStop(new D.SchemeColor(new D.Tint() { Val = 37000 },
             new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 35000 },
            new D.GradientStop(new D.SchemeColor(new D.Tint() { Val = 15000 },
             new D.SaturationModulation() { Val = 350000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 100000 }
            ),
            new D.LinearGradientFill() { Angle = 16200000, Scaled = true }),
          new D.NoFill(),
          new D.PatternFill(),
          new D.GroupFill()),
          new D.LineStyleList(
          new D.Outline(
            new D.SolidFill(
            new D.SchemeColor(
              new D.Shade() { Val = 95000 },
              new D.SaturationModulation() { Val = 105000 }) { Val = D.SchemeColorValues.PhColor }),
            new D.PresetDash() { Val = D.PresetLineDashValues.Solid })
          {
              Width = 9525,
              CapType = D.LineCapValues.Flat,
              CompoundLineType = D.CompoundLineValues.Single,
              Alignment = D.PenAlignmentValues.Center
          },
          new D.Outline(
            new D.SolidFill(
            new D.SchemeColor(
              new D.Shade() { Val = 95000 },
              new D.SaturationModulation() { Val = 105000 }) { Val = D.SchemeColorValues.PhColor }),
            new D.PresetDash() { Val = D.PresetLineDashValues.Solid })
          {
              Width = 9525,
              CapType = D.LineCapValues.Flat,
              CompoundLineType = D.CompoundLineValues.Single,
              Alignment = D.PenAlignmentValues.Center
          },
          new D.Outline(
            new D.SolidFill(
            new D.SchemeColor(
              new D.Shade() { Val = 95000 },
              new D.SaturationModulation() { Val = 105000 }) { Val = D.SchemeColorValues.PhColor }),
            new D.PresetDash() { Val = D.PresetLineDashValues.Solid })
          {
              Width = 9525,
              CapType = D.LineCapValues.Flat,
              CompoundLineType = D.CompoundLineValues.Single,
              Alignment = D.PenAlignmentValues.Center
          }),
          new D.EffectStyleList(
          new D.EffectStyle(
            new D.EffectList(
            new D.OuterShadow(
              new D.RgbColorModelHex(
              new D.Alpha() { Val = 38000 }) { Val = "000000" }) { BlurRadius = 40000L, Distance = 20000L, Direction = 5400000, RotateWithShape = false })),
          new D.EffectStyle(
            new D.EffectList(
            new D.OuterShadow(
              new D.RgbColorModelHex(
              new D.Alpha() { Val = 38000 }) { Val = "000000" }) { BlurRadius = 40000L, Distance = 20000L, Direction = 5400000, RotateWithShape = false })),
          new D.EffectStyle(
            new D.EffectList(
            new D.OuterShadow(
              new D.RgbColorModelHex(
              new D.Alpha() { Val = 38000 }) { Val = "000000" }) { BlurRadius = 40000L, Distance = 20000L, Direction = 5400000, RotateWithShape = false }))),
          new D.BackgroundFillStyleList(
          new D.SolidFill(new D.SchemeColor() { Val = D.SchemeColorValues.PhColor }),
          new D.GradientFill(
            new D.GradientStopList(
            new D.GradientStop(
              new D.SchemeColor(new D.Tint() { Val = 50000 },
                new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 },
            new D.GradientStop(
              new D.SchemeColor(new D.Tint() { Val = 50000 },
                new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 },
            new D.GradientStop(
              new D.SchemeColor(new D.Tint() { Val = 50000 },
                new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 }),
            new D.LinearGradientFill() { Angle = 16200000, Scaled = true }),
          new D.GradientFill(
            new D.GradientStopList(
            new D.GradientStop(
              new D.SchemeColor(new D.Tint() { Val = 50000 },
                new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 },
            new D.GradientStop(
              new D.SchemeColor(new D.Tint() { Val = 50000 },
                new D.SaturationModulation() { Val = 300000 }) { Val = D.SchemeColorValues.PhColor }) { Position = 0 }),
            new D.LinearGradientFill() { Angle = 16200000, Scaled = true }))) { Name = "Office" });

        theme.Append(themeElements);
        theme.Append(new D.ObjectDefaults());
        theme.Append(new D.ExtraColorSchemeList());

        themePart.Theme = theme;
        presentationPart.AddPart(themePart, "rId5");
        #endregion

        P.Picture picture = new P.Picture
                            (
                                new P.NonVisualPictureProperties
                                (
                                    new P.NonVisualDrawingProperties() { Id = (UInt32Value)1026U, Name = "Photo", Description = "" },
                                    new P.NonVisualPictureDrawingProperties
                                    (
                                        new D.PictureLocks() { NoChangeAspect = true }
                                    ),
                                    new ApplicationNonVisualDrawingProperties()
                                ),
                                new D.BlipFill
                                (
                                    new D.Blip
                                        (
                                            new D.NonVisualPicturePropertiesExtensionList()
                                        ) { Embed = "rId2" },
                                    new D.Stretch
                                    (
                                        new D.FillRectangle()
                                    )
                                ),
                                new P.ShapeProperties
                                (
                                    new D.Transform2D
                                    (
                                        new D.Offset() { X = 1 * cm, Y = 3 * cm },
                                        new D.Extents() { Cx = 16 * cm, Cy = 14 * cm }
                                    ),
                                    new D.PresetGeometry
                                    (
                                        new D.AdjustValueList()
                                    ) { Preset = D.ShapeTypeValues.Rectangle }
                                )
                            );

        slidePart.Slide.CommonSlideData.ShapeTree.AppendChild<P.Picture>(picture);



        //Close the presentation handle
        presentationDoc.Close();
    }
publicstaticvoidcreatepresentation(字符串文件路径)
{
//在指定的文件路径创建演示文稿。默认情况下,演示文稿文档类型为pptx。
PresentationDocument presentationDoc=PresentationDocument.Create(文件路径,PresentationDocumentType.Presentation);
PresentationPart PresentationPart=presentationDoc.AddPresentationPart();
presentationPart.Presentation=新演示文稿();
SlideMasterIdList sldMasterIdList=新的SlideMasterIdList(新的SlideMasterId(){Id=(UINT32值)2147483648U,RelationshipId=“rId1”});
slidedlist sldIdList=新的slidedlist();
SlideSize sldSize=new SlideSize(){Cx=2751*mm/10,Cy=1905*mm/10,Type=SlideSizeValues.Screen4x3};//DIN A4-横向。
NotesSize NotesSize=new NotesSize(){Cx=1905*mm/10,Cy=2751*mm/10};//DIN A4-纵向。
DefaultTextStyle DefaultTextStyle=新的DefaultTextStyle();
presentationPart.Presentation.Append(sldMasterIdList、sldIdList、sldSize、notesSize、defaultTextStyle);
#区域[幻灯片部分]
SlidePart SlidePart=presentationPart.AddNewPart(“rId2”);
AppendChild(新的SlideId(){Id=(uint32值)256U,RelationshipId=“rId2”});
slidePart.Slide=新幻灯片
(
新公共数据
(
新成形机
(
新的P.NonVisualGroupShapeProperties
(
新的P.NonVisualDrawingProperties(){Id=(UINT32值)1U,Name=“”},
新的P.非可视组ShapedRawingProperties(),
新应用程序NonVisualDrawingProperties()
),
新的GroupShapeProperties(新的DocumentFormat.OpenXml.Drawing.TransformGroup())
)
),
新建ColorMapOverride(新建MasterColorMapping())
);
ImagePart ip=slidePart.AddImagePart(ImagePartType.Jpeg,“rId2”);
MyStream.Position=0;
ip.FeedData(MyStream);
#端区
#区域[幻灯片部分]
SlideLayoutPart SlideLayoutPart=slidePart.AddNewPart(“rId1”);
SlideLayout SlideLayout=新SlideLayout
(
新公共数据
(
新成形机
(
新的P.NonVisualGroupShapeProperties
(
新的P.NonVisualDrawingProperties(){Id=(UINT32值)1U,Name=“”},
新的P.非可视组ShapedRawingProperties(),
新应用程序NonVisualDrawingProperties()
),
新的GroupShapeProperties
(
新的DocumentFormat.OpenXml.Drawing.TransformGroup()
)
)
),
新ColorMapOverride
(
新的MasterColorMapping()
)
);
slideLayoutPart.SlideLayout=SlideLayout;
#端区
#区域[slideMasterPart]
SlideMasterPart SlideMasterPart=slideLayoutPart.AddNewPart(“rId1”);
SlideMaster SlideMaster=新SlideMaster(
新数据(新形状视图)(
新的P.NonVisualGroupShapeProperties(
新的P.NonVisualDrawingProperties(){Id=(UINT32值)1U,Name=“”},
新的P.非可视组ShapedRawingProperties(),
新应用程序NonVisualDrawingProperties()),
新的GroupShapeProperties(新的DocumentFormat.OpenXml.Drawing.TransformGroup()),
新P.形状(
新P.非视觉形状属性(
新的P.NonVisualDrawingProperties(){Id=(UInt32Value)2U,Name=“Title Placeholder 1”},
新的P.NonVisualShapeDrawingProperties(新的ShapeLocks(){NoGrouping=true}),
新应用程序NonVisualDrawingProperties(新占位符形状(){Type=PlaceholderValues.Title})),
新的P.ShapeProperties(),
新P.TextBody(
新的BodyProperties(),
新建ListStyle(),
新段落()),
新P.ColorMap(){Background1=D.ColorSchemeIndexValues.Light1,Text1=D.ColorSchemeIndexValues.Dark1,Background2=D.ColorSchemeIndexValues.Light2,Text2=D.ColorSchemeIndexValues.Dark2,Accent1=D.ColorSchemeIndexValues.Accent1,Accent2=D.ColorSchemeIndexValues.Accent2,Accent3=D.ColorSchemeIndexValues.Accent3,Accent4=D.ColorSchemeIndexValues.Accent4,Accent5=D.ColorSchemeIndexValues.Accent5,Accent6=D.ColorSchemeIndexValues.Accent6,Hyperlink=D.ColorSchemeIndexValues.Hyperlink,FollowedHyperlink=D.ColorSchemeIndexValues.FollowedHyperlink},
新建SlideLayoutId列表(新建SlideLayoutId(){Id=(uint32值)2147483649U,RelationshipId=“rId1”}),
新文本样式(