国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#向PPT文檔插入圖片以及導出圖片的實例

2020-01-24 00:52:20
字體:
來源:轉載
供稿:網友

 PowerPoint演示文稿是我們日常工作中常用的辦公軟件之一,而圖片則是PowerPoint文檔的重要組成部分,那么如何向幻燈片插入圖片以及導出圖片呢?本文我將給大家分享如何使用一個免費版PowerPoint組件―Free Spire.Presentation,以C#/VB.NET編程的方式來快速地實現這兩個功能。我們可以從官網下載Free Spire.Presentation,創建項目后添加此DLL作為引用。

插入圖片

向PPT文檔插入圖片時,這里我選擇插入兩張圖片到不同的兩張幻燈片中。

具體步驟:

在之前需要添加以下命名空間:

using Spire.Presentation;using Spire.Presentation.Drawing;

步驟1:新建一個PPT文檔。

Presentation presentation = new Presentation();presentation.Slides.Append();

步驟2:插入第一張圖片到第一張幻燈片

string ImageFile = @"C:/Users/Administrator/Pictures/01.jpg";RectangleF rect = new RectangleF(350, 100, 300, 250);  presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;

步驟3:添加形狀,再添加文本到形狀里面。

RectangleF rect2 = new RectangleF(50, 100, 300, 250);IAutoShape shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, rect2);shape.Fill.FillType = FillFormatType.None;shape.ShapeStyle.LineColor.Color = Color.White;//添加文本到形狀中shape.TextFrame.Text = "大熊貓是哺乳動物,已在地球上生存了至少800萬年,被譽為活化石和中國國寶,世界自然基金會的形象大使,是世界生物多樣性保護的旗艦物種。據第三次全國大熊貓野外種群調查,全世界野生大熊貓已不足1600只,屬于中國國家一級保護動物。";TextRange textRange = shape.TextFrame.TextRange;shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left;//設置文本字體textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;textRange.Fill.SolidColor.Color = Color.Black;textRange.LatinFont = new TextFont("Arial Black"

步驟4:同樣,插入第二張圖片到第二張幻燈片,添加形狀,再添加文本到形狀里面。最后保存文檔。

presentation.SaveToFile(@"C:/Users/Administrator/Desktop/result.pptx ", FileFormat.Pptx2010);System.Diagnostics.Process.Start(@"C:/Users/Administrator/Desktop/result.pptx ");

效果圖:

 

全部代碼:

 using System; using System.Drawing; using System.Windows.Forms; using Spire.Presentation; using Spire.Presentation.Drawing;  namespace InsertimageinPowerPointFille {  public partial class Form : Form  {   public Form()   {    InitializeComponent();   }    private void button_Click(object sender, EventArgs e)   {    //新建PPT    Presentation presentation = new Presentation();    presentation.Slides.Append();     //插入第一張圖片到第一張幻燈片    string ImageFile = @"C:/Users/Administrator/Pictures/.jpg";    RectangleF rect = new RectangleF(, , , );    presentation.Slides[].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);    presentation.Slides[].Shapes[].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;     //添加形狀    RectangleF rect = new RectangleF(, , , );    IAutoShape shape = presentation.Slides[].Shapes.AppendShape(ShapeType.Rectangle, rect);    shape.Fill.FillType = FillFormatType.None;    shape.ShapeStyle.LineColor.Color = Color.White;     //添加文本到形狀中    shape.TextFrame.Text = "大熊貓是哺乳動物,已在地球上生存了至少萬年,被譽為活化石和中國國寶,世界自然基金會的形象大使,是世界生物多樣性保護的旗艦物種。據第三次全國大熊貓野外種群調查,全世界野生大熊貓已不足只,屬于中國國家一級保護動物。";    TextRange textRange = shape.TextFrame.TextRange;    shape.TextFrame.Paragraphs[].Alignment = TextAlignmentType.Left;     //設置文本字體    textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;    textRange.Fill.SolidColor.Color = Color.Black;    textRange.LatinFont = new TextFont("Arial Black");     //插入第二張圖片到第二張幻燈片    string ImageFile = @"C:/Users/Administrator/Pictures/.jpg";    RectangleF rect = new RectangleF(, , , );    presentation.Slides[].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);    presentation.Slides[].Shapes[].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;     //添加形狀    RectangleF rect = new RectangleF(, , , );    IAutoShape shape = presentation.Slides[].Shapes.AppendShape(ShapeType.Rectangle, rect);    shape.Fill.FillType = FillFormatType.Solid;    shape.Fill.FillType = FillFormatType.None;    shape.ShapeStyle.LineColor.Color = Color.White;     //添加文本到形狀中    shape.TextFrame.Text = "黑白相間的外表,有利隱蔽在密林的樹上和積雪的地面而不易被天敵發現。相對鋒利的爪和發達有力的前后肢,有利于大熊貓能快速爬上高大的喬木。";    TextRange textRange = shape.TextFrame.TextRange;     //設置文本字體    textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;    textRange.Fill.SolidColor.Color = Color.Blue;    textRange.LatinFont = new TextFont("Arial Black");     //保存文件    presentation.SaveToFile(@"C:/Users/Administrator/Desktop/result.pptx ", FileFormat.Pptx);    System.Diagnostics.Process.Start(@"C:/Users/Administrator/Desktop/result.pptx ");   }  } }

從上面的代碼可以發現,其實通過這個組件,我們還可以自由地設置我們想要的形狀、文本、字體、顏色等等,用起來確實方便又快速。感興趣的話可以試一下其他豐富的效果。

導出圖片

現在,我們導出上述運行后文檔的圖片。

具體步驟:

同樣添加如下命名空間:

using Spire.Presentation;

步驟1: 新建一個Presentation對象,并加載Presentation文件。

Presentation ppt = new Presentation();ppt.LoadFromFile(@"C:/Users/Administrator/Desktop/result.pptx");

 步驟2:遍歷PPT文檔所有的圖片,并保存為.png格式。

for (int i = 0; i < ppt.Images.Count; i++)   {    Image image = ppt.Images[i].Image;    image.Save(string.Format(@"../../Images{0}.png", i));   }

效果圖:

 

全部代碼:

 using System; using System.Drawing; using System.Windows.Forms; using Spire.Presentation;  namespace ExtractImagesfromPPT {  public partial class Form : Form  {   public Form()   {    InitializeComponent();   }    private void button_Click(object sender, EventArgs e)   {    Presentation ppt = new Presentation();    ppt.LoadFromFile(@"C:/Users/Administrator/Desktop/result.pptx");    for (int i = ; i < ppt.Images.Count; i++)    {     Image image = ppt.Images[i].Image;     image.Save(string.Format(@"../../Images{}.png", i));     }   }  } }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 芦山县| 沁源县| 保靖县| 吉林省| 石河子市| 获嘉县| 九江市| 桐柏县| 资溪县| 肥西县| 陇南市| 句容市| 基隆市| 新密市| 土默特左旗| 谢通门县| 北辰区| 武威市| 崇阳县| 新建县| 泸溪县| 神木县| 彰武县| 易门县| 苍南县| 安吉县| 同江市| 灵璧县| 五台县| 赤城县| 丰台区| 沅陵县| 望城县| 汝城县| 威海市| 永宁县| 宝应县| 平邑县| 晋江市| 桐柏县| 鄂托克前旗|