根据项目要求,我们需要将Word文档中的图像转换为位图对象。为了实现这一目标,我们尝试将Microsoft.Office.Interop.Word dll中的inlineshape对象转换为位图。然而,我们无法成功,因为获取到的剪贴板对象为空。请查看我们尝试的代码如下:
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图像转换为位图?如果您能指导我们如何将Word文档中的图像转换为位图对象,那将是非常有帮助的。