将图片从单词保存到文件夹C#

时间:2012-09-05 18:51:41

标签: c# ms-word image

所以我已经想出如何用link

替换带有文字的图片

但现在我需要将图片从单词导出到文件夹。我猜每当我找到一个对象的图片时(类型s = Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)我应该用它做点什么...但我找不到选项:s。 saveAsPicture(@ “C:\ pic.jpg”);

1 个答案:

答案 0 :(得分:4)

您的问题可能与以下内容重复:extract image from word file

但是,基于my previous answer您的问题,如何以编程方式将外部图像与Word中的内嵌形状进行比较(请参阅Compare picture in Word file and in a folder?) - 您可以进行一些简单的调整并使用几乎完全相同的相同的示例代码,用于将每个内联形状导出到文件夹,而不是将形状与另一个图像进行比较。

为了说明我已经为您做了必要的调整并提供了以下源代码。同样,该应用程序是基于.NET 4.5,Microsoft Office Object Library 15.0版和Microsoft Word Object Library 15.0版的C#控制台应用程序。

和以前一样,我在源代码中包含了引用,这样您就可以看到我根据我的解决方案获得了哪些来源(并且这些来源获得了他们应得的信用;)

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));
                }
            }
        }
    }
}