从转换的Word文档图像转换成位图对象位图、转换成、图像、对象

2023-09-03 00:36:51 作者:仅剩旳温纯

由于每个项目的要求,我们需要从word文档图像转换成位图对象。为了实现这一目标,我们试图inlineshape对象转换的Microsoft.Office.Interop.Word DLL成位图。但是无法获得成功,得到剪贴板对象为空。请找到code,我们尝试了如下;

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

有没有人曾在转换文字图像转换成位图?这将是很大的帮助,如果你能指导我们如何与转换从Word文档图像转换成位图对象继续前进。

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.

推荐答案

解决这个职位: http://stackoverflow.com/ A /一百零七万一千二百十二分之七百九十三万七千五百九十零 这是一个与STAThread一个问题:

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

Thread thread = new Thread([Method]);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();