【问题标题】:extract image from word file从word文件中提取图像
【发布时间】:2011-12-17 18:13:12
【问题描述】:

我一直在尝试使用以下 C# 代码从 doc 文件中提取图像,但它不起作用:

object missing = System.Reflection.Missing.Value;            
            Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document oDoc = new Microsoft.Office.Interop.Word.Document();
            oWord.Visible = false;
            object str1 = "C:\\doc.doc";
            oDoc = oWord.Documents.Open(ref str1, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

            if (oDoc.InlineShapes.Count > 0)            {


                for (int j = 0; j < oDoc.InlineShapes.Count; j++)
                {  

                    oWord.ActiveDocument.Select();
                    oDoc.ActiveWindow.Selection.CopyAsPicture();

                    IDataObject data = Clipboard.GetDataObject();                    

                    if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
                    {
                        object bm = data.GetData(DataFormats.Bitmap);

                        Bitmap bmp;
                        bmp = (Bitmap)data.GetData(typeof(System.Drawing.Bitmap));

                        bmp.Save("C:\\test.bmp");
                    }



                }

谁能给出从word文件中提取图像的正确代码?

【问题讨论】:

  • 如果您可以管理DOCX 文件,它们就是简单的压缩文件。你可以打开它们找到二进制文件,然后阅读 XML 索引来找出你想要的那个。

标签: c# excel-interop


【解决方案1】:

我在使用 spire 库时遇到了同样的问题,我得到了解决方案,我提供了该库的链接,只需在您的 Visual Studio 中添加该 dll 文件并复制以下代码:

enter code here



        if (file.ShowDialog() == DialogResult.OK) //if there is a file choosen by the user  
        {
            object path = file.FileName; //get the path of the file  
            object readOnly = true;

            Spire.Doc.Document document = new Spire.Doc.Document(file.FileName);
            int index = 1;

            //Get Each Section of Document  
            foreach (Spire.Doc.Section section in document.Sections)
            {
                //Get Each Paragraph of Section  
                foreach (Spire.Doc.Documents.Paragraph paragraph in section.Paragraphs)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(paragraph.Text);//storing the text of word in string builder
                    Console.WriteLine(sb);
                    //Get Each Document Object of Paragraph Items  
                    foreach (DocumentObject docObject in paragraph.ChildObjects)
                    {
                        //If Type of Document Object is Picture, Extract.  
                        if (docObject.DocumentObjectType == DocumentObjectType.Picture)
                        {
                            DocPicture pic = docObject as DocPicture;

                            String imgName = String.Format(@"E:\C#\OnlineExam\Question\{0}.png", index);

                            //Save Image  
                            pic.Image.Save(imgName, System.Drawing.Imaging.ImageFormat.Png);
                            index++;
                        }
                    }
                }
            }}

You can find dll files from this link

【讨论】:

    【解决方案2】:

    如果是.docx 文件,则另一种选择:

    1. 将文件重命名为.zip
    2. 提取内容
    3. 在解压的word/media文件夹中查找以下目录

    是的,这不是发布的 C# 方式,但即使编写代码来执行上述 3 个步骤,如果这正是您所寻找的,这也是一种自动化流程的方式。

    【讨论】:

      【解决方案3】:

      这是一个本地/非网页版本。

      大部分代码都复制自:http://www.csharphelp.com/2007/05/save-picture-from-clipboard-to-file-using-c/ - 加上 Ekk 的回答中的几行。

      InlineShape inlineShape = m_word.ActiveDocument.InlineShapes[m_i];
      inlineShape.Select();
      m_word.Selection.Copy();
      if (Clipboard.GetDataObject() != null)
      {
          IDataObject data = Clipboard.GetDataObject();
      
          if (data.GetDataPresent(DataFormats.Bitmap))
          {
              Image image = (Image)data.GetData(DataFormats.Bitmap,true);
      
              image.Save("image.bmp",System.Drawing.Imaging.ImageFormat.Bmp);
              image.Save("image.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
              image.Save("image.gif",System.Drawing.Imaging.ImageFormat.Gif);
          }
          else
          {
              MessageBox.Show("The Data In Clipboard is not as image format");
          }
      }
      else
      {
          MessageBox.Show("The Clipboard was empty");
      }
      

      【讨论】:

      • 顺便说一句,当 Word 正在为相关图像打开上下文菜单时,这将不起作用。复制后剪贴板不会有任何数据。
      【解决方案4】:
      using System;
      using System.Drawing;
      using System.IO;
      using System.Threading;
      using Page = System.Web.UI.Page;
      using Microsoft.Office.Interop.Word;
      using Microsoft.VisualBasic.Devices;
      public partial class ReadIMG : System.Web.UI.Page
      {   
          private Application m_word;
          private int m_i;
          protected void Page_Load(object sender, EventArgs e)
          {
              object missing = Type.Missing;
              object FileName = Server.MapPath("~/LectureOrig/Word.docx");
              object readOnly = true;
              m_word = new Application();
              m_word.Documents.Open(ref FileName,
                                      ref missing, ref readOnly, ref missing, ref missing,
                                      ref missing, ref missing, ref missing, ref missing,
                                      ref missing, ref missing, ref missing, ref missing, ref missing,ref missing,ref missing);
              try
              {
                  for (int i = 1; i <= m_word.ActiveDocument.InlineShapes.Count; i++)
                  {
                      m_i = i;
                     // CopyFromClipboardShape();
                      Thread thread = new Thread(CopyFromClipbordInlineShape);
                      thread.SetApartmentState(ApartmentState.STA);
                      thread.Start();
                      thread.Join();
                  }
              }
              finally
              {
                  object save = false;
                  m_word.Quit(ref save, ref missing, ref missing);
                  m_word = null;
              }
          }
          protected void CopyFromClipbordInlineShape()
          {   
              InlineShape inlineShape = m_word.ActiveDocument.InlineShapes[m_i];
              inlineShape.Select();
              m_word.Selection.Copy();
              Computer computer = new Computer();
              //Image img = computer.Clipboard.GetImage();
              if (computer.Clipboard.GetDataObject() != null)
              {
                  System.Windows.Forms.IDataObject data = computer.Clipboard.GetDataObject();
                  if (data.GetDataPresent(System.Windows.Forms.DataFormats.Bitmap))
                  {
                      Image image = (Image)data.GetData(System.Windows.Forms.DataFormats.Bitmap, true);                
                      image.Save(Server.MapPath("~/ImagesGet/image.gif"), System.Drawing.Imaging.ImageFormat.Gif);
                      image.Save(Server.MapPath("~/ImagesGet/image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
      
                  }
                  else
                  {
                      LabelMessage.Text="The Data In Clipboard is not as image format";
                  }
              }
              else
              {
                  LabelMessage.Text="The Clipboard was empty";
              }
          }
      

      来自How To Exctract images from Doc (Word) file in C#?的代码副本

      【讨论】:

      • 命名空间未使用 Page = System.Web.UI.Page; 定义
      • 当我们从 Word 复制图像时,它会降低质量。我玩过很多选择,但它不起作用。你知道如何解决这个问题吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2011-06-01
      相关资源
      最近更新 更多