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.

C#

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;

VB.NET

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.

C#

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

VB.NET

            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.

C#

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

VB.NET
            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.

C#

                        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);
                            index++;
                        }
                    }
                }
            }

VB.NET

                        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.

C#

            System.Diagnostics.Process.Start(“Image-0.png”);

VB.NET

            System.Diagnostics.Process.Start(“Image-0.png”)

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.

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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