C#向PPT文檔插入圖片以及導(dǎo)出圖片的實(shí)例
PowerPoint演示文稿是我們?nèi)粘9ぷ髦谐S玫霓k公軟件之一,而圖片則是PowerPoint文檔的重要組成部分,那么如何向幻燈片插入圖片以及導(dǎo)出圖片呢?本文我將給大家分享如何使用一個(gè)免費(fèi)版PowerPoint組件—Free Spire.Presentation,以C#/VB.NET編程的方式來(lái)快速地實(shí)現(xiàn)這兩個(gè)功能。我們可以從官網(wǎng)下載Free Spire.Presentation,創(chuàng)建項(xiàng)目后添加此DLL作為引用。
插入圖片
向PPT文檔插入圖片時(shí),這里我選擇插入兩張圖片到不同的兩張幻燈片中。
具體步驟:
在之前需要添加以下命名空間:
using Spire.Presentation; using Spire.Presentation.Drawing;
步驟1:新建一個(gè)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 = "大熊貓是哺乳動(dòng)物,已在地球上生存了至少800萬(wàn)年,被譽(yù)為活化石和中國(guó)國(guó)寶,世界自然基金會(huì)的形象大使,是世界生物多樣性保護(hù)的旗艦物種。據(jù)第三次全國(guó)大熊貓野外種群調(diào)查,全世界野生大熊貓已不足1600只,屬于中國(guó)國(guó)家一級(jí)保護(hù)動(dòng)物。"; TextRange textRange = shape.TextFrame.TextRange; shape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Left; //設(shè)置文本字體 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 = "大熊貓是哺乳動(dòng)物,已在地球上生存了至少萬(wàn)年,被譽(yù)為活化石和中國(guó)國(guó)寶,世界自然基金會(huì)的形象大使,是世界生物多樣性保護(hù)的旗艦物種。據(jù)第三次全國(guó)大熊貓野外種群調(diào)查,全世界野生大熊貓已不足只,屬于中國(guó)國(guó)家一級(jí)保護(hù)動(dòng)物。"; TextRange textRange = shape.TextFrame.TextRange; shape.TextFrame.Paragraphs[].Alignment = TextAlignmentType.Left; //設(shè)置文本字體 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 = "黑白相間的外表,有利隱蔽在密林的樹(shù)上和積雪的地面而不易被天敵發(fā)現(xiàn)。相對(duì)鋒利的爪和發(fā)達(dá)有力的前后肢,有利于大熊貓能快速爬上高大的喬木。"; TextRange textRange = shape.TextFrame.TextRange; //設(shè)置文本字體 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 "); } } }
從上面的代碼可以發(fā)現(xiàn),其實(shí)通過(guò)這個(gè)組件,我們還可以自由地設(shè)置我們想要的形狀、文本、字體、顏色等等,用起來(lái)確實(shí)方便又快速。感興趣的話可以試一下其他豐富的效果。
導(dǎo)出圖片
現(xiàn)在,我們導(dǎo)出上述運(yùn)行后文檔的圖片。
具體步驟:
同樣添加如下命名空間:
using Spire.Presentation;
步驟1: 新建一個(gè)Presentation對(duì)象,并加載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)); } } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:詳解StackExchange.Redis通用封裝類分享
本文標(biāo)題:C#向PPT文檔插入圖片以及導(dǎo)出圖片的實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6067.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10C#實(shí)現(xiàn)簡(jiǎn)單合并word文檔的方法
- 01-10C#簡(jiǎn)單實(shí)現(xiàn)子窗體向父窗體傳值的方法
- 01-10C#代碼實(shí)現(xiàn)PDF文檔操作類
- 01-10C#實(shí)現(xiàn)向多線程傳參的三種方式實(shí)例分析
- 01-10C#編程實(shí)現(xiàn)四舍五入、向上及下取整的方法
- 01-10C#數(shù)據(jù)結(jié)構(gòu)之雙向鏈表(DbLinkList)實(shí)例詳解
- 01-10C#獲取遠(yuǎn)程XML文檔的方法
- 01-10解析C#面向?qū)ο缶幊讨蟹椒ǎ╩ethod)的使用
- 01-10如何使用C#從word文檔中提取圖片


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)