Greeed
Greeed

Reputation: 419

Save pictures from word to folder C#

So I have figured out how to replace pictures with text in word using this link

But now I need to export pictures from word to a folder. And I'm guessing whenever I find a picture which is a object(type s=Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture) I should do something with it... But I can't find the option: s.saveAsPicture(@"C:\pic.jpg");

Upvotes: 0

Views: 8214

Answers (1)

Lasse Christiansen
Lasse Christiansen

Reputation: 10325

Your question might be a possible duplicate of: extract image from word file

However, based on my previous answer to your question regarding how to programmatically compare an external image with inline shapes in Word ( see Compare picture in Word file and in a folder? ) - you can make a couple of simple adjustments and use almost the exact same sample code to export each inline shape to a folder instead of comparing the shapes with another image.

To illustrate I have made the necessary adjustments for you and provided the source code below. Again, the application is a C# console application based on .NET 4.5, Microsoft Office Object Library version 15.0, and Microsoft Word Object Library version 15.0.

And like before, I have included references in the source code such that you can see which sources I have based my solution on ( and such that the sources gets the credit they deserve ;) )

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Word.Application;

namespace WordDocStats
{
    class Program
    {
        // General idea is based on: https://stackoverflow.com/a/7937590/700926
        static void Main()
        {
            // Open a doc file
            var wordApplication = new Application();
            var document = wordApplication.Documents.Open(@"C:\Users\Username\Documents\document.docx");

            // For each inline shape, export it to a file
            // By inspection you can see that the first inline shape have index 1 ( and not zero as one might expect )
            for (var i = 1; i <= wordApplication.ActiveDocument.InlineShapes.Count; i++)
            {
                // closure
                // http://confluence.jetbrains.net/display/ReSharper/Access+to+modified+closure
                var inlineShapeId = i; 

                // parameterized thread start
                // https://stackoverflow.com/a/1195915/700926
                var thread = new Thread(() => SaveInlineShapeToFile(inlineShapeId, wordApplication));

                // STA is needed in order to access the clipboard
                // https://stackoverflow.com/a/518724/700926
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }

            // Close word
            wordApplication.Quit();
            Console.ReadLine();
        }

        // General idea is based on: https://stackoverflow.com/a/7937590/700926
        protected static void SaveInlineShapeToFile(int inlineShapeId, Application wordApplication)
        {
            // Get the shape, select, and copy it to the clipboard
            var inlineShape = wordApplication.ActiveDocument.InlineShapes[inlineShapeId];
            inlineShape.Select();
            wordApplication.Selection.Copy();

            // Check data is in the clipboard
            if (Clipboard.GetDataObject() != null)
            {
                var data = Clipboard.GetDataObject();

                // Check if the data conforms to a bitmap format
                if (data != null && data.GetDataPresent(DataFormats.Bitmap))
                {
                    // Fetch the image and convert it to a Bitmap
                    var image = (Image) data.GetData(DataFormats.Bitmap, true);
                    var currentBitmap = new Bitmap(image);

                    // Save the bitmap to a file
                    currentBitmap.Save(@"C:\Users\Username\Documents\" + String.Format("img_{0}.png", inlineShapeId));
                }
            }
        }
    }
}

Upvotes: 5

Related Questions