Image

Images in Slides In Presentations

Images make presentations more engaging and interesting. In Microsoft PowerPoint, you can insert pictures from a file, the internet, or other locations onto slides. Similarly, Aspose.Slides allows you to add images to slides in your presentations through different procedures.

Aspose.Slides supports operations with images in these popular formats: JPEG, PNG, GIF, and others.

Adding Images Stored Locally to Slides

You can add one or several images on your computer onto a slide in a presentation. This sample code in C++ shows you how to add an image to a slide:

auto pres = System::MakeObject<Presentation>();

auto slide = pres->get_Slides()->idx_get(0);
auto image = pres->get_Images()->AddImage(File::ReadAllBytes(u"image.png"));
slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 10.0f, 10.0f, 100.0f, 100.0f, image);

pres->Save(u"pres.pptx", SaveFormat::Pptx);

Adding Images From the Web to Slides

If the image you want to add to a slide is unavailable on your computer, you can add the image directly from the web.

This sample code shows you how to add an image from the web to a slide in C++:

auto pres = System::MakeObject<Presentation>();
auto slide = pres->get_Slides()->idx_get(0);
    
auto webClient = System::MakeObject<WebClient>();
auto imageData = webClient->DownloadData(System::MakeObject<Uri>(u"[REPLACE WITH URL]"));

auto image = pres->get_Images()->AddImage(imageData);
slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 10.0f, 10.0f, 100.0f, 100.0f, image);

pres->Save(u"pres.pptx", SaveFormat::Pptx);

Adding Images to Slide Masters

A slide master is the top slide that stores and controls information (theme, layout, etc.) about all slides under it. So, when you add an image to a slide master, that image appears on every slide under that slide master.

This C++ sample code shows you how to add an image to a slide master:

auto pres = System::MakeObject<Presentation>();
auto slide = pres->get_Slides()->idx_get(0);
auto masterSlide = slide->get_LayoutSlide()->get_MasterSlide();

auto image = pres->get_Images()->AddImage(File::ReadAllBytes(u"image.png"));
masterSlide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 10.0f, 10.0f, 100.0f, 100.0f, image);

pres->Save(u"pres.pptx", SaveFormat::Pptx);

Adding Images as Slide Background

You may decide to use a picture as the background for a specific slide or several slides. In that case, you have to see Setting Images as Backgrounds for Slides.

Inserting/Adding SVG into Presentations

You can add or insert any image into a presentation by using the AddPictureFrame method that belongs to the IShapeCollection interface.

To create an image object based on SVG image, you can do it this way:

  1. Create SvgImage object to insert it to ImageShapeCollection
  2. Create PPImage object from ISvgImage
  3. Create PictureFrame object using IPPImage interface

This sample code shows you how to implement the steps above to add an SVG image into a presentation:

// The path to the documents directory
System::String dataDir = u"D:\\Documents\\";

// Source SVG file name
System::String svgFileName = dataDir + u"sample.svg";

// Output presentation file name
System::String outPptxPath = dataDir + u"presentation.pptx";

// Create new presentation
auto p = System::MakeObject<Presentation>();

// Read SVG file content
System::String svgContent = File::ReadAllText(svgFileName);

// Create SvgImage object
System::SharedPtr<ISvgImage> svgImage = System::MakeObject<SvgImage>(svgContent);

// Create PPImage object
System::SharedPtr<IPPImage> ppImage = p->get_Images()->AddImage(svgImage);

// Creates a new PictureFrame 
p->get_Slides()->idx_get(0)->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 200.0f, 100.0f, static_cast<float>(ppImage->get_Width()), static_cast<float>(ppImage->get_Height()), ppImage);

// Save presentation in PPTX format
p->Save(outPptxPath, SaveFormat::Pptx);

Converting SVG to a Set of Shapes

Aspose.Slides' conversion of SVG to a set of shapes is similar to the PowerPoint functionality used to work with SVG images:

PowerPoint Popup Menu

The functionality is provided by one of the overloads of the AddGroupShape method of the IShapeCollection interface that takes an ISvgImage object as the first argument.

This sample code shows you how to use the described method to convert an SVG file to a set of shapes:

// The path to the documents directory
System::String dataDir = u"D:\\Documents\\";

// Source SVG file name
System::String svgFileName = dataDir + u"sample.svg";

// Output presentation file name
System::String outPptxPath = dataDir + u"presentation.pptx";

// Create new presentation
System::SharedPtr<IPresentation> presentation = System::MakeObject<Presentation>();

// Read SVG file content
System::String svgContent = File::ReadAllText(svgFileName);

// Create SvgImage object
System::SharedPtr<ISvgImage> svgImage = System::MakeObject<SvgImage>(svgContent);

// Get slide size
System::Drawing::SizeF slideSize = presentation->get_SlideSize()->get_Size();

// Convert SVG image to group of shapes scaling it to slide size
presentation->get_Slides()->idx_get(0)->get_Shapes()->AddGroupShape(svgImage, 0.f, 0.f, slideSize.get_Width(), slideSize.get_Height());

// Save presentation in PPTX format
presentation->Save(outPptxPath, SaveFormat::Pptx);

Adding Images as EMF in Slides

Aspose.Slides for C++ allows you to generate EMF images from excel sheets and add the images as EMF in slides with Aspose.Cells.

This sample code shows you how to perform the described task:

System::String dataDir = u"D:\\Documents\\";

StringPtr cellsXls = new String(dataDir.ToWCS().c_str());
cellsXls->Append(L"chart.xls");
intrusive_ptr<Aspose::Cells::IWorkbook> book = Aspose::Cells::Factory::CreateIWorkbook(cellsXls);

intrusive_ptr<Aspose::Cells::IWorksheet> sheet = book->GetIWorksheets()->GetObjectByIndex(0);
intrusive_ptr<Aspose::Cells::Rendering::IImageOrPrintOptions> options = Aspose::Cells::Factory::CreateIImageOrPrintOptions();
options->SetHorizontalResolution(200);
options->SetVerticalResolution(200);
options->SetImageFormat(Aspose::Cells::Systems::Drawing::Imaging::ImageFormat::GetEmf());

// Save the workbook to stream
intrusive_ptr<Aspose::Cells::Rendering::ISheetRender> sr = Aspose::Cells::Factory::CreateISheetRender(sheet, options);

System::SharedPtr<Presentation> pres = System::MakeObject<Presentation>();

pres->get_Slides()->RemoveAt(0);

System::String EmfSheetName;
for (int32_t j = 0; j < sr->GetPageCount(); j++)
{
    EmfSheetName = dataDir + u"test" + System::String::FromWCS(sheet->GetName()->value()) + u" Page" + (j + 1) + u".out.emf";
    sr->ToImage(j, new String(EmfSheetName.ToWCS().c_str()));

    auto bytes = System::IO::File::ReadAllBytes(EmfSheetName);
    auto emfImage = pres->get_Images()->AddImage(bytes);

    System::SharedPtr<ISlide> slide = pres->get_Slides()->AddEmptySlide(pres->get_LayoutSlides()->GetByType(SlideLayoutType::Blank));
    auto slideSize = pres->get_SlideSize()->get_Size();
    slide->get_Shapes()->AddPictureFrame(ShapeType::Rectangle, 0.0f, 0.0f, slideSize.get_Width(), slideSize.get_Height(), emfImage);
}

pres->Save(dataDir + u"Saved.pptx", SaveFormat::Pptx);