从word文件中提取图像
我一直在尝试使用以下 C# 代码从 doc 文件中提取图像,但它不起作用:
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oDoc = new Microsoft.Office.Interop.Word.Document();
oWord.Visible = false;
object str1 = "C:\\doc.doc";
oDoc = oWord.Documents.Open(ref str1, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (oDoc.InlineShapes.Count > 0) {
for (int j = 0; j < oDoc.InlineShapes.Count; j++)
{
oWord.ActiveDocument.Select();
oDoc.ActiveWindow.Selection.CopyAsPicture();
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
{
object bm = data.GetData(DataFormats.Bitmap);
Bitmap bmp;
bmp = (Bitmap)data.GetData(typeof(System.Drawing.Bitmap));
bmp.Save("C:\\test.bmp");
}
}
任何人都可以提供从 word 文件中提取图像的正确代码吗?
I have been trying the following C# code to extract image from the doc file but it is not working:
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document oDoc = new Microsoft.Office.Interop.Word.Document();
oWord.Visible = false;
object str1 = "C:\\doc.doc";
oDoc = oWord.Documents.Open(ref str1, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (oDoc.InlineShapes.Count > 0) {
for (int j = 0; j < oDoc.InlineShapes.Count; j++)
{
oWord.ActiveDocument.Select();
oDoc.ActiveWindow.Selection.CopyAsPicture();
IDataObject data = Clipboard.GetDataObject();
if (data.GetDataPresent(typeof(System.Drawing.Bitmap)))
{
object bm = data.GetData(DataFormats.Bitmap);
Bitmap bmp;
bmp = (Bitmap)data.GetData(typeof(System.Drawing.Bitmap));
bmp.Save("C:\\test.bmp");
}
}
Can anybody give the proper code for extracting the image from word file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
代码复制自How To Extract images from Doc (Word) file in C#?
Code copy from How To Exctract images from Doc (Word) file in C#?
如果是
.docx
文件,则还有另一种选择:.zip
是的,它不是按照发布的方式执行此操作的 C# 方法,但如果您正在寻找的话,即使编写代码来执行上述 3 个步骤也将是一种自动化该过程的方法。
Another option if it's a
.docx
file:.zip
Yeah, it's not the C# way to do it as posted, but even writing the code to perform the 3 steps above would be a way of automating the process if that's what you are looking for.
这是本地/非网页版本。
此代码大部分复制自: http://www.csharphelp.com/2007/05/save-picture-from-clipboard-to-file-using-c/ - 加上Ekk答案中的几行。
Here's a local/non-web-page version.
Most of this code is copied from: http://www.csharphelp.com/2007/05/save-picture-from-clipboard-to-file-using-c/ - plus a few lines from Ekk's answer.
我使用 spire 库时遇到了同样的问题,我得到了解决方案,我给出了该库使用的链接,只需在 Visual Studio 中添加该 dll 文件并复制以下代码:
您可以从此链接找到 dll 文件
I had the same problem I used spire library and i got the solution i am giving the link of that library use just add that dll files in your visual studio and copy the below code :
You can find dll files from this link