Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting images from word document into bitmap object

As per project requirement we need to convert images from word document into bitmap object. To achieve this we tried to convert inlineshape object from Microsoft.Office.Interop.Word dll into bitmap. However unable to get success, getting clipboard object as null. Please find the code which we tried as below;

using System.Drawing;
using Microsoft.Office.Interop.Word;
namespace WordApp1
{
    class Program
    {
        static void Main(string[] args)
        {
           Application wordApp = (Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
           Documents documents = wordApp.Documents;

           Document d = null;
           foreach (Document document in documents)
           {
              if (document.ActiveWindow.Caption.Contains("{Word document name}"))
              {
                 d = document;
              }
           }

           foreach (InlineShape shape in d.InlineShapes)
           {
              shape.Range.Select();
              d.ActiveWindow.Selection.Range.CopyAsPicture();
              System.Windows.Forms.IDataObject dobj = System.Windows.Forms.Clipboard.GetDataObject();  //Getting clipboard object as null
              if(dobj.GetDataPresent(typeof(System.Drawing.Bitmap)))
              {
                 Bitmap bmp;
                 System.IO.MemoryStream ms = new System.IO.MemoryStream();
                 bmp = (Bitmap)dobj.GetData(typeof(System.Drawing.Bitmap));
              }
            }
        }        
     }
 }

Does anyone has worked on converting word images into bitmap? It would be great help if you could guide us how to go ahead with converting images from word document into bitmap object.

like image 200
NewAutoUser Avatar asked Nov 05 '22 03:11

NewAutoUser


1 Answers

Resolved in this post: https://stackoverflow.com/a/7937590/1071212 It's a problem with STAThread:

Thread thread = new Thread([Method]);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
like image 182
aclalex Avatar answered Nov 09 '22 06:11

aclalex