Articles in this section
Category / Section

How to extract Images from Word document in C# and VB?

4 mins read

Syncfusion Essential DocIO is a .NET Word Library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can extract the images from Word document using C#.

You can find image by using FindAllItemsByProperty API, find all the images in the Word document.

Steps to extract images from Word document in C#

  1. Create a new C# console application project.

Create a new C# console application.

  1. Install Syncfusion.DocIO.WinForms NuGet package as a reference to your .NET Framework applications from the NuGet.org.

Install Syncfusion.DocIO.WinForms NuGet package

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.

  1. Include the following namespace in the Program.cs file.
    C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS; 

VB

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS 
  1. Use the following code to extract images from Word document.
    C#
// Open the file as a stream.
using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../Data/Template.docx"), FileMode.Open, FileAccess.Read))            
{            
   // Load the file stream into a Word document.            
   using (WordDocument document = new WordDocument(docStream, FormatType.Docx))            
   {            
       // Find all pictures by EntityType in the Word document.            
       List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);            

       // Iterate through the pictures and save each one as an image file.            
       for (int i = 0; i < pictures.Count; i++)            
       {            
           WPicture image = pictures[i] as WPicture;            

           // Use a MemoryStream to handle the image bytes from the picture.            
           using (MemoryStream memoryStream = new MemoryStream(image.ImageBytes))            
           {            
               // Define the path where the image will be saved.            
               string imagePath = Path.GetFullPath(@"../../Image-" + i + ".jpeg");            

               // Create a FileStream to write the image to the specified path.            
               using (FileStream filestream = new FileStream(imagePath, FileMode.Create, FileAccess.Write))            
               {            
                   memoryStream.CopyTo(filestream);            
               }            
           }            
       }            
   }            
} 

VB

' Open the file as a stream.
Using docStream As FileStream = New FileStream(Path.GetFullPath("../../Data/Template.docx"), FileMode.Open, FileAccess.Read)
    ' Load the file stream into a Word document.
    Using document As WordDocument = New WordDocument(docStream, FormatType.Docx)
        ' Find all pictures by EntityType in the Word document.
        Dim pictures As List(Of Entity) = document.FindAllItemsByProperty(EntityType.Picture, Nothing, Nothing)

        ' Iterate through the pictures and save each one as an image file.
        For i As Integer = 0 To pictures.Count - 1
            Dim image As WPicture = TryCast(pictures(i), WPicture)

            ' Use a MemoryStream to handle the image bytes from the picture.
            Using memoryStream As MemoryStream = New MemoryStream(image.ImageBytes)
                ' Define the path where the image will be saved.
                Dim imagePath As String = Path.GetFullPath("../../Image-" & i & ".jpeg")

                ' Create a FileStream to write the image to the specified path.
                Using filestream As FileStream = New FileStream(imagePath, FileMode.Create, FileAccess.Write)
                    memoryStream.CopyTo(filestream)
                End Using
            End Using
        Next
    End Using
End Using 

You can download a complete working sample to extract images from Word document from GitHub

Input template Word document as follows:

Input Word document

By executing the program, you will get the images in given folder as like below:

Extracted images from Word document using DocIO

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

See Also:

Working with Images

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied