2011-02-02 70 views
5

根据项目需求,我们需要将图像从word文档转换为位图对象。为了达到这个目的,我们尝试将inlinehape对象从Microsoft.Office.Interop.Word dll转换成位图。但无法取得成功,请将剪贴板对象设为空。请找到我们尝试的代码如下:将图像从word文档转换为位图对象

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文档转换为位图对象,那将会非常有帮助。

回答

0

试试这个。

foreach (InlineShape shape in d.InlineShapes)    
{ 
    if (shape != null) 
    { 
     shape.Range.Select(); 
     d.ActiveWindow.Selection.Copy(); 
     Bitmap bitmap = new Bitmap(Clipboard.GetImage()); 
    } 
} 
0

有两大剪贴板。

通常我们会用System.Windows.Forms.Clipboard,但它很糟糕。

改为使用System.Windows.Clipboard,只需将PresentationCore添加到您的参考中即可。

(在我的情况下,C:\ Program Files \ Reference Assemblies \ Microsoft \ Framework.NETFramework \ v4.0 \ Profile \ Client \ PresentationCore.dll)