Free Support Forum - aspose.com

Aspose.Words for C++,extract image from word (doc/docx)

I need to use C++ to extract pictures from a word document. I don’t know which function in aspose.words api to use。can you help me?

@NanaSeino

Please read the following article about extracting pictures from the Word document. Hope this helps you.
How to Extract Images from a Document

There seems to be no code sample on this page.

What I also want to know is what format the extracted pictures are saved on the disk.

Thank you for your help.

@NanaSeino

Please refresh the documentation page to see the all content. We are sharing the code example in this post for your kind reference.

// The path to the documents directories.
System::String inputDataDir = GetInputDataDir_WorkingWithImages();
System::String outputDataDir = GetOutputDataDir_WorkingWithImages();
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Image.SampleImages.doc");

System::SharedPtr<NodeCollection> shapes = doc->GetChildNodes(NodeType::Shape, true);
int32_t imageIndex = 0;

for (System::SharedPtr<Shape> shape : System::IterateOver<System::SharedPtr<Shape>>(shapes))
{
    if (shape->get_HasImage())
    {
        System::String imageFileName = System::String::Format(u"Image.ExportImages.{0}.{1}", imageIndex, FileFormatUtil::ImageTypeToExtension(shape->get_ImageData()->get_ImageType()));
        System::String imagePath = outputDataDir + imageFileName;
        shape->get_ImageData()->Save(imagePath);
        std::cout << "Image saved at " << imagePath.ToUtf8String() << std::endl;
        imageIndex++;
    }
}

The above code example saves the image with original image type. If you want to save the image with some other image type, please use ShapeBase.GetShapeRenderer method to create an object that can be used to render this shape into an image. This method returns object of ShapeRenderer class. ShapeRenderer.Save method is used to save the image with desired image type.

Following code example shows how to export shapes to files in the local file system using a shape renderer.

// Open a document that contains shapes and get its shape collection
auto doc = MakeObject<Document>(MyDir + u"Various shapes.docx");
SharedPtr<System::Collections::Generic::List<SharedPtr<Shape>>> shapes =
    doc->GetChildNodes(NodeType::Shape, true)->LINQ_Cast<SharedPtr<Shape>>()->LINQ_ToList();

 
for (auto shape : System::IterateOver(doc->GetChildNodes(NodeType::Shape, true)->LINQ_OfType<SharedPtr<Shape>>()))
{
    SharedPtr<ShapeRenderer> renderer = shape->GetShapeRenderer();
    auto options = MakeObject<ImageSaveOptions>(SaveFormat::Png);
    renderer->Save(ArtifactsDir + String::Format(u"Shape.RenderAllShapes.{0}.png", shape->get_Name()), options);
}

Similarly, I also want to know how to use C++ to extract pictures from powerpoint , which function in Aspose.slides.api should be used? Can you give me sample code if it is convenient?

@NanaSeino

We are working over your requirements for extracting images from PowerPoint presentation and will share the sample code with you as soon as possible.

@NanaSeino

Please find the sample project attached for your convenience to extract image from shapes of presentation slides.

ExtractingImagesfromPresentationShapes.zip (6.2 KB)