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

Gre*_*eed 0 c# ms-word image

所以我想出了如何使用这个链接在word中用文字替换图片

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

Las*_*sen 5

您的问题可能与以下内容重复:从 word 文件中提取图像

但是,根据我之前对您关于如何以编程方式将外部图像与 Word 中的内嵌形状进行比较的问题的回答(请参阅比较 Word 文件和文件夹中的图片?) - 您可以进行一些简单的调整并使用几乎完全相同的将每个内嵌形状导出到文件夹而不是将形状与另一个图像进行比较的相同示例代码。

为了说明我已经为您进行了必要的调整,并在下面提供了源代码。同样,该应用程序是基于 .NET 4.5、Microsoft Office 对象库 15.0 版和 Microsoft Word 对象库 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: /sf/answers/555631331/
        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
                // /sf/answers/83714081/
                var thread = new Thread(() => SaveInlineShapeToFile(inlineShapeId, wordApplication));

                // STA is needed in order to access the clipboard
                // /sf/answers/36310711/
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }

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

        // General idea is based on: /sf/answers/555631331/
        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));
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)