• Home
  • Product-Life
  • PDF
  • Excel
  • Word
  • My Music Box
It's a long road...

HTML-to-RTF Pro DLL .Net

A standalone .Net class library for C#, VB.Net, J# and ColdFusuion developers. 

Learn More

C# Word – Extract Image from Word

4/10/2012

1 Comment

 
How to Extract Image from Word
After you embedded an image into a Word document, you will find that image becomes part of that document. It's not easy to extract image from Word document to re-use. Microsoft office does not provide any easy to use option to perform such action. Even you do your best and get the image out you may cry because it has lost all sorts of quality. However, extracting image from Word for re-use is not insurmountable. If you happen to have problems on extracting images from Word document, follow this post to extract the image as its full resolution.

This solution requires some basic programming skills. With the help of Spire.Doc, a powerful and all-in-one .NET component, we can do this job effortlessly. Download Spire.Doc and install on system. Follow the simple guide below to extract image from Word document now.

Step 1 Create Project
Create a C# project in visual studio add Spire.Doc.dll as reference. The default setting of Spire.Doc.dll is placed under "C:\Program Files\e-iceblue\Spire.Doc\Bin”. Select assembly Spire.Doc.dll and click OK to add it to the project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Interface;
using Spire.Doc.Collections;
using Spire.Doc.Fields;

namespace extractimage

{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

Step 2 Open Word Document
Put the Word document into the project folder. Use the code below to load and open in the project.

            //Open a word document
            Document document = new Document(@"..\..\Sample.doc");
            int index = 0;

Step 3 Extract Image
There are 3 steps to extract image from Word. First create a queue and then put the document objects in the queue. At last judge the object type.

            //Create a new queue
            Queue<ICompositeObject> containers = new Queue<ICompositeObject>();

            //Put the document objects in the queue
            containers.Enqueue(document);
            while (containers.Count > 0)
            {
                ICompositeObject container = containers.Dequeue();
                DocumentObjectCollection docObjects = container.ChildObjects;
                foreach (DocumentObject docObject in docObjects)
                {
                    //Judge the object type
                    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++;
                    }
                    else
                    {
                        if (docObject is ICompositeObject)
                        {
                            containers.Enqueue(docObject as ICompositeObject);
                        }
                    }
                }
            }

Step 4 Save Image
Spire.Doc allows users to extract image from Word document and save as popular image format such as PNG, JPEG, BMP, Tiff, etc. 

Picture
Press F5 to start the project and the image will be extracted from Word document. We can find it in project folder bin -> debug.

Original Image in Word Document:

Picture
Output Image:

Picture
More about Spire.Doc

As a professional and powerful Word component, Spire.Doc doesn’t need Microsoft Office Word Automation but also allows user to directly operate Word document, format and style and insert content to Word document. Spire.Doc which with high quality, powerful functions and cheap price supports Word 97, Word 2003, Word 2007 and Word 2010. Click to learn more...
1 Comment
Joseph Aidan link
8/16/2012 06:14:33 pm

Excellent! I admire all the helpful data you've shared in your articles. I'm looking forward for more helpful articles from you. :)

Joseph Aidan
www.arielmed.com

Reply



Leave a Reply.

    Categories

    全部
    Add Header/Footer
    Add Watermark
    C# Add Images
    C# Word Hyperlinks
    C# Word Table
    C# Word Text Color
    Encrypt Word
    Extract Word Image
    Html To Image
    Html To Word
    Insert Page Break
    Insert Word Borders
    Merge Word Documents
    Set Word Properties
    Word Footnote
    Word To Epub
    Word To Html
    Word To Pdf

    RSS Feed

    Recommend Blog:


    Blu-ray/DVD Backup
    Office Tricks
    No.1 Software Pick

Powered by
✕