How to Extract Image from Word with C#/VB.NET

In order to make contents to be more vivid and appealed, people would like to insert images in Word document. For example, if one document introduces one book, the author will insert book cover in paragraphs.

Although these images are often used to decorate document, some are very helpful to explain contents in other documents if the same information appears in two documents. In this post, I want to introduce a method about how to extract image from Word document by using C#/VB.NET.

In this example, I prepare a Word document which includes two images. I will extract and save them. Then, launch one of them.

Also, I use a component, Spire.Doc to realize this function quickly. So I add its dll file as reference in project.

Detailed Steps Shown as Following:           

At the beginning, we need to use the following namespace.


using System;
using System.Drawing;
using System.Collections;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Collections;
using Spire.Doc.Interface;
using Spire.Doc.Fields;


Imports System
Imports System.Drawing
Imports System.Collections
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Collections
Imports Spire.Doc.Interface
Imports Spire.Doc.Fields 

Step 1. Load document and then set index for following image name setting.


            Document document = new Document(@”E:\work\Documents\Antarctic.docx”);
            int index = 0;


            Dim document As New Document(“E:\work\Documents\Antarctic.docx”)
            Dim index As Integer = 0

Step 2. Use foreach sentence to get sections in document, paragraphs in section and child objects in paragraph.


            foreach (Section section in document.Sections)
                foreach (Paragraph paragraph in section.Paragraphs)
                    foreach (DocumentObject docObject in paragraph.ChildObjects)

            For Each section As Section In document.Sections
                For Each paragraph As Paragraph In section.Paragraphs
                    For Each docObject As DocumentObject In paragraph.ChildObjects

Step 3. Use if sentence to judge object type in paragraphs. If the type is image, get it and set image name format with String.Format(“Image-{0}.png”, index) to make that image name can be changed by order automatically. Finally, save image by using picture.Image.Save() method. There are two parameters passed to this method, image name and image format.


                        if (docObject.DocumentObjectType == DocumentObjectType.Picture)
                            DocPicture picture = docObject as DocPicture;
                            //Name the image
                            String imageName = String.Format(“Image-{0}.png”, index);
                            //Save the image.
                            picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png);


                        If docObject.DocumentObjectType = DocumentObjectType.Picture Then
                            Dim picture As DocPicture = TryCast(docObject, DocPicture)
                            ‘Name the image
                            Dim imageName As String = String.Format(“Image-{0}.png”, index)
                            ‘Save the image.
                            picture.Image.Save(imageName, System.Drawing.Imaging.ImageFormat.Png)
                            index += 1
                        End If
                    Next docObject
                Next paragraph
            Next section

Step 4. Launch one of the images which are extracted from Word. The image I will launch is the first image.





Result Shown as Following:


Click Here to LEARN MORE about Spire.Doc

Click Here to DOWNLOAD Spire.Doc

Spire.Office also can be used to realize this function.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s