New*_*ser 5 .net c# interop ms-word image
根据项目要求,我们需要将图像从word文档转换为位图对象.为此,我们尝试将inlineshape对象从Microsoft.Office.Interop.Word dll转换为位图.但无法获得成功,将剪贴板对象设为null.请找到我们尝试过的代码如下;
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));
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有人致力于将单词图像转换为位图?如果您可以指导我们如何继续将图像从word文档转换为位图对象,那将是非常有帮助的.
这篇文章已解决:/sf/answers/555631331/ 这是 STAThread 的问题:
Thread thread = new Thread([Method]);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
Run Code Online (Sandbox Code Playgroud)