Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active November 30, 2021 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/ca012b5e162ecd752c0c4dd990ca2089 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/ca012b5e162ecd752c0c4dd990ca2089 to your computer and use it in GitHub Desktop.
Extract Images from MS Word Documents using C#
// 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