Extract images from word documents using C#

Images play an essential role in illustrating key information in Word documents. They enhance the document’s attractiveness and improve its overall presentation. As a programmer, you may be tasked with extracting the images embedded within Word DOCX or DOC documents. In this C# Aspose.Words image extraction tutorial, we will explore how to extract images from Word documents programmatically using C# and the Aspose Plugin. You will also learn how to save the extracted images to your desired location.

.NET API for Image Extraction from Word Documents

To extract images from Microsoft Word DOCX/DOC documents, we will use Aspose.Words for .NET. It is a powerful and widely used API for creating and manipulating Word documents and can be complemented by the $99 Plugin for additional features. You can download the API’s DLL and add its reference to your application. Alternatively, you can install it directly from NuGet using the package manager console.

PM> Install-Package Aspose.Words

Extract Images from a Word Document in C#

In Word documents, images are represented as shapes. Therefore, to extract images from protected Word documents or standard DOCX files, you must process all the shapes in the document. The following steps outline how to extract images from DOCX in C# programmatically.

The following code sample demonstrates how to extract images from a Word document in C#:

// Load the document
Document doc = new Document("input.docx");

// Get all shapes that contain images
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

// Extract and save each image
foreach (Shape shape in shapes)
{
    if (shape.ImageData.HasImage)
    {
        // Extract image file path
        string imagePath = $"Image_{shape.Name}.png";
        shape.ImageData.Save(imagePath);
    }
}

Try Aspose.Words for .NET for Free

You can get a free temporary license to try Aspose.Words for .NET without any limitations. Get your temporary license now.

Conclusion

Images have become an integral part of Word documents, making the content visually appealing. Therefore, Aspose.Words for .NET, complemented by the .NET library for extracting images from Word, provides a wide range of features to add or manipulate images in Word documents.

This article specifically covered the extraction of images from Word documents using C#. With the help of a code sample, you have learned how to programmatically extract all images from a Word DOCX/DOC file and save them to a folder. You can read more about Aspose.Words for .NET using the documentation. If you have any questions, feel free to reach out via our forum.

See Also

Tip: If you ever need to convert a Word document from a PowerPoint presentation, consider using the Aspose Presentation to Word Document converter.