What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 6745 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > Extract Images from Word File with C#, VB.NET ...
Lacy

Extract Images from Word File with C#, VB.NET

 Blog author: Lacy | Posted on: 5/27/2012 | Category: C# Blogs | Views: 1193 | Status: [Member] | Points: 75 | Alert Moderator   


Extract content from a document is always needed in people's daily work. Now I want to share a method to extract images from a word document with C#, VB.NET. The whole process is very easy. The main method can be finished only by three steps. 

How to extract images from Word file with C#, VB.NET

In my method, I use an MS word component Spire.Doc to finish this task. Spire. Doc supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight and doesn't need Microsoft Office Word Automation. 


Step1. Create a new project.

Create a new project in Visual Studio and set the Target framework to be .NET Framework 4.


Step2. Add reference.

1.     Add System Drawing and Spire.Doc dll as references in Project.

2.     Add the below namespace at the top of the method.

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

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

Step3. Extract images from word document.

1.     Load a word document from system.
C# Code:
            //Open a word document.
            Document document = new Document(@"D:\michelle\JaneEyre.doc");
            int index = 0;

VB.NET Code:
                 'Open a word document.
                 Dim document As New Document("D:\michelle\JaneEyre.doc ")
                 Dim index As Integer = 0

2.     Extract images from word document
C# Code:
            //Create a new queue.
            Queue containers = new Queue();
            //Put the document objects in the queue.
            containers.Enqueue(document);
            while (containers.Count > 0)
            {
                ICompositeObject container = (ICompositeObject)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);
                        }
                    }
                }
            }

VB.NET Code:
                     'Create a new queue.
                     Dim containers As New Queue(Of ICompositeObject)()
                     'Put the document objects in the queue.
                     containers.Enqueue(document)
                     Do While containers.Count > 0
                           Dim container As ICompositeObject = DirectCast(containers.Dequeue(), ICompositeObject)
                           Dim docObjects As DocumentObjectCollection = container.ChildObjects
                           For Each docObject As DocumentObject In docObjects
                                  'Judge the object type. 
                                  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
                                  Else
                                         If TypeOf docObject Is ICompositeObject Then
                                                containers.Enqueue(TryCast(docObject, ICompositeObject))
                                         End If
                                  End If
                           Next docObject
                     Loop

Preview
                               
                                                  Word File
                                                              
                                                                               Extracted Images


Spire.Doc can extract images in word document easily and save the images to different commonly used formats such as png, bmp, jpg, gif, tif and so on.




Found interesting? Add this to:


About Keating Arly

Experience:1 year(s)
Home page:http://www.e-iceblue.com
Member since:Tuesday, March 27, 2012
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points

More Blogs

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2013 6:01:09 PM