【问题标题】:Save pictures from word to folder C#将图片从word保存到文件夹C#
【发布时间】:2012-09-05 18:51:40
【问题描述】:

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

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

【问题讨论】:

  • 没有这样的选项。你可以把它放到剪贴板,然后用它做你想做的事,social.msdn.microsoft.com/Forums/en-SG/vsto/thread/…
  • 将整个文档保存为 html 感觉如何?这会将所有图像保存在一个单独的文件夹中。这似乎是 MVP 推荐的。
  • @Remou 非常无脑的解决方案,只有在我们不必对 word 文档执行任何其他操作的情况下才有效......但是在我的情况下,我必须比较图片,分析它们替换它们所以我不能暴力破解这个解决方案

标签: c# ms-word image


【解决方案1】:

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

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

为了说明我已经为你做了必要的调整,并提供了下面的源代码。同样,该应用程序是基于 .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: 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));
                }
            }
        }
    }
}

【讨论】:

  • 我遵循了这个答案,第一次效果很好。但是现在我遇到了一些奇怪的事情:剪贴板包含数据,但它不会让我以任何可用格式获取数据。就像我说的,位图在我第一次使用时就起作用了。但是现在带有任何 DataFormats.SOMETHING 的 data.GetDataPresent(...) 总是返回 false。知道会发生什么吗?
  • 这是超级旧的,但如果 Word 正在为相关图像打开上下文菜单,这将不起作用。
  • 您好,如何在 .Net Core 2.2 中实现这一点?那里没有剪贴板机制,我需要从 Word 应用程序中提取图像,你有什么想法吗?非常感谢。
猜你喜欢
  • 2015-06-24
  • 2015-11-02
  • 1970-01-01
  • 2020-08-15
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
相关资源
最近更新 更多