2

Some people can't be convinced to send screenshots as pictures. Instead they paste the screenshots into a Word document and send that document. It is possible to right click a bitmap in Word and choose "Save as picture...", but it's too much work for many bitmaps.

Therefore I'm looking for a tool that can extract bitmaps in their original resolution from a Word document. It should work offline on Windows 7 x64 and be gratis. It can be a command line tool.

1

Syncfusion provides file parsing libraries for Word - Essential DocIO that can also be used for this purpose.

Example

Essential DocIO extracted image

private void ExtractImagesFromWordDocument(string inputFilePath,string outputFolderLocation)
    {
        //Create Word document instance.
        WordDocument document = new WordDocument();
        //Open Existing Word document
        document.Open(inputFilePath);

        int index = 0;
        foreach (var item in document.Visit())
        {
            //If the entity is picture then save at particular location
            if (item.EntityType == EntityType.Picture)
            {
                WPicture pic = (WPicture)item;
                pic.Image.Save(outputFolderLocation + "Image" + index + ".png", ImageFormat.Png);
                index++;
            }
        }
        document.Close();
    }

The whole suite of controls and libraries is available for free through the community license program if you qualify (less than 1 million USD in revenue). Note: I work for Syncfusion.

0

Perhaps I'm a bit impatient and I should wait for an answer for a longer time, but I just had the need to get this done...

Using the great but hardly documented DocX library for C# I was able to solve that task and create a small console application that extracts all images from a document into a folder. According to my short tests, all images are in PNG format when pasted from clipboard.

The following code works for .docx files only, which is ok for me.

using System;
using System.IO;
using Novacode;

namespace BmpFromDocx
{
    static class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                PrintHelp();
                return;
            }
            var doc = DocX.Load(args[0]);
            var index = 0;
            foreach (var image in doc.Images)
            {
                var stream = image.GetStream(FileMode.Open, FileAccess.Read);
                var buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                File.WriteAllBytes(Path.Combine(args[1], $"image{index}.png"), buffer);
                index++;
            }
        }

        private static void PrintHelp()
        {
            Console.WriteLine("Usage: BmpFromDocx <docx> <output path>");
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.