c# - 如何读取带图片的word文件

标签 c# .net

目前我正在开发一个在线考试项目,我想阅读一个包含图像的word文件。我有一个逐行读取 word 文件的代码,但我不知道如何从 word 文件中读取图像。

我的代码:

word = new Microsoft.Office.Interop.Word.Application();
object miss = System.Reflection.Missing.Value;
OpenFileDialog file = new OpenFileDialog();
if (file.ShowDialog() == DialogResult.OK) 
{
    object path = file.FileName; //get the path of the file  
    MessageBox.Show(path.ToString());
    object readOnly = true;
    Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, 
        ref miss, 
        ref readOnly, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss, 
        ref miss);
    string totaltext = "";
    for (int i = 0; i < docs.Paragraphs.Count; i++)
    {
        totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
    }
    Console.WriteLine(totaltext);
    docs.Close();
    word.Quit();
}

最佳答案

引用这个 link 你可以找到这段代码,它可能会帮助你获得 图片来自word文档

using System;
using System.Drawing;
using System.IO;
using System.Threading;
using Microsoft.Office.Interop.Word;
using Microsoft.VisualBasic.Devices;
using Page=System.Web.UI.Page;

public partial class DefaultPage : Page {
    private Application m_word;
    private int m_i;

    protected void Page_Load(object sender, EventArgs e) {
        object missing = Type.Missing;
        object fileName = "C:\\Test.docx";
        m_word = new Application();
        m_word.Documents.Open(ref fileName,
                              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);

        try {
            for (int i = 0; i < m_word.ActiveDocument.InlineShapes.Count; i++) {
                m_i = i;
                Thread thread = new Thread(CopyFromClipboardInlineShape);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }
            for (int i = 0; i < m_word.ActiveDocument.Shapes.Count; i++) {
                m_i = i;
                Thread thread = new Thread(CopyFromClipboardShape);
                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 CopyFromClipboardInlineShape() {
        InlineShape inlineShape = m_word.ActiveDocument.InlineShapes[m_i];
        inlineShape.Select();
        m_word.Selection.Copy();
        Computer computer = new Computer();
        Image img = computer.Clipboard.GetImage();
        /*...*/
    }

    protected void CopyFromClipboardShape() {
        object missing = Type.Missing;
        object i = m_i + 1;
        Shape shape = m_word.ActiveDocument.Shapes.get_Item(ref i);
        shape.Select(ref missing);
        m_word.Selection.Copy();
        Computer computer = new Computer();
        Image img = computer.Clipboard.GetImage();
        /*...*/
    }
}

关于c# - 如何读取带图片的word文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53626698/

相关文章:

c# - 创建参数类型的对象

c# - 向字符串类添加扩展方法 - C#

c# - 使用 fluentmigrator 运行 sql 脚本

.net - .Net框架可以免费用于商业用途吗?

c# - 在 LINQ 中重新排序记录

c# - 服务器违反了协议(protocol)。 Section=ResponseHeader Detail=CR 后面必须跟着 LF

c# - 使用类从 asp.net 中的数据库填充下拉列表的方法是什么?

c# - 无法实例化泛型类

.net - WPF 从对象的 DataTable 绑定(bind)到 DataGrid

.net - Windows Azure 辅助角色未通过第一行代码