none
How to copy Images from word document?

    Question

  • Good day to all,

    Hi all, 

    I use the below code to read a image from folder and to copy it with new word file and it works properly. but I get a task to read a images(Figures) from word document and copy it to new word file. 

    Anyone have idea regarding this?

    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("C:\Documents and Settings\Word.doc");   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;                     
    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();        
    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("C:/Images/image.gif"), System.Drawing.Imaging.ImageFormat.Gif);                image.Save(Server.MapPath("C:/Images/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";        }    
    }
    }

    Note: 
    In a word document images are not in a particular format(while try to copy in a folder it exist a scrap format) 

    Monday, April 30, 2012 6:13 AM

Answers

  • Hi Harvest,

    Welcome to the MSDN forum.

    I think you can check the below sample, it can extract images from word document. You can combine this sample with your code above.

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private Microsoft.Office.Interop.Word.Application m_word;
            private int m_i;
            private void Form1_Load(object sender, EventArgs e)
            {
                object missing = Type.Missing;
                object FileName = @"d:\testd.docx";
                object readOnly = true;
                m_word = new Microsoft.Office.Interop.Word.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;
                    ((_Application)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(@"d:\xx1.gif", System.Drawing.Imaging.ImageFormat.Gif);
                        image.Save(@"d:\xx2.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    
                    }
                    else
                    {
                        MessageBox.Show("The Data In Clipboard is not as image format");
                    }
                }
                else
                {
                    MessageBox.Show("The Data In Clipboard is not as image format");
                }
            }
        }

    Have a nice day.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 02, 2012 5:31 AM