C # Insert a picture into a PPT document and export an instance of a picture _c# tutorial

Source: Internet
Author: User
Tags save file

PowerPoint presentations are one of the most common office software in our daily work, and pictures are an important part of PowerPoint documents, so how do you insert pictures into slides and export pictures? This article I will share with you how to use a free version of the PowerPoint component-free spire.presentation to C#/VB. NET programming approach to achieve these two functions quickly. We can download free spire.presentation from the official website and add this DLL as a reference after creating the project.

Insert Picture

When I insert a picture into a PPT document, I choose to insert two pictures into two different slides.

Specific steps:

Before you need to add the following namespaces:

Using Spire.presentation;
Using Spire.Presentation.Drawing;

Step 1: Create a new PPT document.

Presentation presentation = new presentation ();

Presentation. Slides.append ();

Step 2: Insert the first picture into slide one

String imagefile = @ "C:\Users\Administrator\Pictures\01.jpg";
RectangleF rect = new RectangleF (n, MB, MB);  
Presentation. Slides[0]. Shapes.appendembedimage (Shapetype.rectangle, ImageFile, rect);
Presentation. Slides[0]. Shapes[0]. Line.FillFormat.SolidFillColor.Color = Color.floralwhite;

Step 3: Add the shape, and then add the text to the shape.

RectangleF rect2 = new RectangleF (m, N,);
Iautoshape shape = presentation. Slides[0]. Shapes.appendshape (Shapetype.rectangle, rect2);
Shape. Fill.filltype = Fillformattype.none;
Shape. ShapeStyle.LineColor.Color = Color.White;

Add text to shapes in shape
. Textframe.text = "Giant Panda is a mammal, has been living on the earth for at least 8 million years, known as a living fossil and China National Treasure, the World Nature Foundation's image ambassador, is the world's biodiversity conservation flagship species." According to the third national Giant Panda Wild Population Survey, the world's wild pandas have less than 1600, belong to China's national level to protect animals. ";
TextRange TextRange = shape. TextFrame.TextRange;
Shape. Textframe.paragraphs[0]. Alignment = Textalignmenttype.left;


Set Text font
textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
TextRange.Fill.SolidColor.Color = Color.Black;
Textrange.latinfont = new Textfont ("Arial Black")

Step 4: Similarly, insert a second picture to slide two, add a shape, and add text to the shape. Finally, save the document.

Presentation. SaveToFile (@ "C:\Users\Administrator\Desktop\result.pptx", fileformat.pptx2010);
System.Diagnostics.Process.Start (@ "C:\Users\Administrator\Desktop\result.pptx");

Effect Chart:

All code:

 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 () {Initializecompo
   Nent (); } private void Button_Click (object sender, EventArgs e) {//new PPT Presentation presentation = new Presenta
    tion (); Presentation.
 
    Slides.append ();
    Insert the first picture to the first slide 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;
    Add Shape RectangleF rect = new RectangleF (,,,); Iautoshape shape = presentation. Slides[].
    Shapes.appendshape (Shapetype.rectangle, rect); Shape.
    Fill.filltype = Fillformattype.none; Shape.
 
   ShapeStyle.LineColor.Color = Color.White; Add text to shapes in shape. Textframe.text = "Giant Panda is a mammal, has lived on the earth for at least a few years, known as living fossil and China National Treasure, the World Nature Foundation's image ambassador, is the world's biodiversity conservation flagship species." According to the third national Giant Panda Wild Population Survey, the world's wild pandas are not enough, belong to China's national level to protect animals.
    "; TextRange TextRange = shape.
    TextFrame.TextRange; Shape. Textframe.paragraphs[].
 
    Alignment = Textalignmenttype.left;
    Set Text font textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
    TextRange.Fill.SolidColor.Color = Color.Black;
 
    Textrange.latinfont = new Textfont ("Arial black");
    Insert the second picture to the second slide 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;
    Add Shape 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; Add text to shapes in shape. Textframe.text = "The Black-and-white appearance, advantageous concealed in the dense forest tree and the snow ground but is not easy to be discovered by the natural enemy." The relatively sharp claws and well-developed front and rear limbs are beneficial for giant pandas to quickly climb tall trees.
    "; TextRange TextRange = shape.
 
    TextFrame.TextRange;
    Set Text font textRange.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Solid;
    TextRange.Fill.SolidColor.Color = Color.Blue;
 
    Textrange.latinfont = new Textfont ("Arial black"); Save file presentation.
    SaveToFile (@ "C:\Users\Administrator\Desktop\result.pptx", fileformat.pptx);
   System.Diagnostics.Process.Start (@ "C:\Users\Administrator\Desktop\result.pptx");

 }
  }
 }

From the above code can be found, in fact, through this component, we can also freely set the shape we want, text, font, color, and so on, it is really convenient and fast. If you are interested, try other rich effects.

Export pictures

Now, let's export the picture of the running document above.

Specific steps:

Also add the following namespaces:

Using Spire.presentation;

Step 1: Create a new Presentation object and load the presentation file.

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

Step 2: Traverse all the pictures in the PPT document and save them in the. png format.

for (int i = 0; i < ppt.) Images.count; i++)

   {

    image image = ppt. Images[i]. Image;

    Image. Save (String. Format (@ ...). \.. \images{0}.png ", i));

   }

Effect Chart:

All code:

 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));}}}
 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.