提取Word文件的图像 [英] extract image from word file

查看:153
本文介绍了提取Word文件的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试下面的C#code从doc文件提取图像,但它不工作:

 对象缺少= System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application oWord =新Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document沃达柯=新Microsoft.Office.Interop.Word.Document();
            oWord.Visible = FALSE;
            对象STR1 =C:\\\\ doc.doc
            沃达柯= oWord.Documents.Open(REF STR1,REF失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪,裁判失踪,失踪参考);            如果(oDoc.InlineShapes.Count大于0){
                对于(INT J = 0; J< oDoc.InlineShapes.Count; J ++)
                {                    oWord.ActiveDocument.Select();
                    oDoc.ActiveWindow.Selection.CopyAsPicture();                    IDataObject的数据= Clipboard.GetDataObject();                    如果(data.GetData present(typeof运算(System.Drawing.Bitmap)))
                    {
                        对象BM = data.GetData(DataFormats.Bitmap);                        BMP位图;
                        BMP =(位图)data.GetData(typeof运算(System.Drawing.Bitmap));                        bmp.Save(C:\\\\ TEST.BMP);
                    }                }

任何人都可以给予适当的code从Word文件中提取的形象呢?


解决方案

 使用系统;
使用System.Drawing中;
使用System.IO;
使用的System.Threading;
使用页面= System.Web.UI.Page;
使用的Microsoft.Office.Interop.Word;
使用Microsoft.VisualBasic.Devices;
公共部分类ReadIMG:System.Web.UI.Page
{
    私人应用m_word;
    私人诠释m_i;
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        反对失踪= Type.Missing;
        目标文件名=使用Server.Mappath(〜/ LectureOrig / Word.docx);
        反对readOnly的= TRUE;
        m_word =新的应用程序();
        m_word.Documents.Open(REF文件名,
                                裁判失踪,裁判只读,已参考失踪,失踪参考,
                                裁判失踪,失踪参考,参考失踪,失踪参考,
                                裁判失踪,失踪参考,参考失踪,失踪参考,参考失踪,失踪参考,参考失踪);
        尝试
        {
            的for(int i = 1; I< = m_word.ActiveDocument.InlineShapes.Count;我++)
            {
                m_i = I;
               // CopyFromClipboardShape();
                线程线程=新主题(CopyFromClipbordInlineShape);
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                的Thread.join();
            }
        }
        最后
        {
            对象保存= FALSE;
            m_word.Quit(REF保存,裁判失踪,失踪参考);
            m_word = NULL;
        }
    }
    保护无效CopyFromClipbordInlineShape()
    {
        InlineShape inlineShape = m_word.ActiveDocument.InlineShapes [m_i]
        inlineShape.Select();
        m_word.Selection.Copy();
        计算机计算机=新计算机();
        //图片IMG = computer.Clipboard.GetImage();
        如果(computer.Clipboard.GetDataObject()!= NULL)
        {
            System.Windows.Forms.IDataObject数据= computer.Clipboard.GetDataObject();
            如果(data.GetData present(System.Windows.Forms.DataFormats.Bitmap))
            {
                形象画像=(图)data.GetData(System.Windows.Forms.DataFormats.Bitmap,真);
                image.Save(使用Server.Mappath(〜/ ImagesGet / Image.gif的),System.Drawing.Imaging.ImageFormat.Gif);
                image.Save(使用Server.Mappath(〜/ ImagesGet / image.jpg的),System.Drawing.Imaging.ImageFormat.Jpeg);            }
            其他
            {
                LabelMessage.Text =数据在剪贴板并不像图片格式;
            }
        }
        其他
        {
            LabelMessage.Text =剪贴板是空的;
        }
    }

在C#

如何Exctract从文档图像(字)文件?

I have been trying the following C# code to extract image from the doc file but it is not working:

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



                }

Can anybody give the proper code for extracting the image from word file?

解决方案

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

Code copy from How To Exctract images from Doc (Word) file in C#?

这篇关于提取Word文件的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆