Learn how to extract images from Word documents in C#: https://blog.aspose.com/2021/11/30/extract-images-from-word-in-csharp/
Last active
November 30, 2021 06:57
-
-
Save aspose-com-gists/ca012b5e162ecd752c0c4dd990ca2089 to your computer and use it in GitHub Desktop.
Extract Images from MS Word Documents using C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the Word document. | |
Document doc = new Document("Document.docx"); | |
// Shape nodes that have the "HasImage" flag set contain and display images. | |
IEnumerable<Shape> shapes = doc.GetChildNodes(NodeType.Shape, true) | |
.OfType<Shape>().Where(s => s.HasImage); | |
int imageIndex = 0; | |
// Loop through shapes. | |
foreach (Shape shape in shapes) | |
{ | |
// Save images. | |
string imageFileName = | |
$"Image_{imageIndex}{FileFormatUtil.ImageTypeToExtension(shape.ImageData.ImageType)}"; | |
shape.ImageData.Save(imageFileName); | |
imageIndex++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment