C# Interop.Word:如何从项目资源文件夹添加图片

4

我在使用 C# 和 Interop.Word 时遇到了一些图片问题。我想要将一张图片添加到我将要生成的文档的页眉中。我已经有了完美运行的代码。

section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddPicture(@"C:\Logo.jpg", ref x, ref x, ref x, ref x, ref x, ref x, ref x);

问题在于代码中无法使用"C:\Logo.jpg",因为发布项目后,用户的C文件夹中很可能没有Logo.jpg。该图像已经在我的项目资源文件夹中。我以前使用过Image.FromFile("Logo.jpg"),但是.AddPicture需要一个字符串而不是一个图像。

有任何想法吗?

-- 编辑 --

在网上看到了这个:

string anyPath = @"C:\logo.jpg";
Properties.Resources.logo.Save(anyPath);
section.Headers.[...].Shapes.AddPicture(anyPath, ...

但是我仍然在GDI+中得到一个通用错误或者是未处理的ExternalException。
5个回答

1

你不能使用...

section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes.AddPicture(@"Logo.jpg", ref x, ref x, ref x, ref x, ref x, ref x, ref x);

使用bin文件夹中图像文件的相对位置,而不是绝对定位的文件

编辑

您可以使用此方法基于相对链接生成绝对文件位置。 需要使用WinForms

string relative = @"images\Logo.jpg";
System.IO.FileInfo fi = new System.IO.FileInfo(Application.ExecutablePath);
string absolute = System.IO.Path.Combine(fi.Directory.FullName, relative);

我认为你更新的方法在Vista/Windows7上可能不太好用,因为它们有各种写入权限(也许),更不用说没有人喜欢随机文件被添加到他们的C盘了。

我认为像这样重要的文件不放在应用程序文件夹中是不好的做法...


我已经尝试过了,但没有任何效果。我还编辑了图像的属性,并将构建操作设置为嵌入资源,将复制到输出目录设置为始终复制,但仍然没有任何变化。 - iamnobody
你有检查过它是否存在吗?例如通过 Console.out.println(File.Exists("logo.jpg")) 这样的操作? - Kurru
它返回false。但文件已经在我的resource.resx和我的资源文件夹中。我现在真的很困惑。如何解决?顺便说一下,谢谢你的时间,Kurru。 :) - iamnobody
你应该查看 {项目文件夹}/bin 目录,看看是否能在那里找到你的图片。如果图片设置为“复制到输出目录”且为“如果较新则复制”,那么它应该会在那里的某个地方。 - Kurru
它在/bin/Debug/Resources和/bin/Release/Resources下面。 - iamnobody
尝试使用字符串relative = @"Resources\Logo.jpg"运行上述代码;这可能有效...使用...Shapes.AddPicture(absolute, ref x, ref x, ref x, ref x, ref x, ref x, ref x); - Kurru

0

您可以使用项目资源。试试这个。

// Copy image to clickboard.
System.Windows.Forms.Clipboard.SetImage( Properties.Resources.your_resource_name);

//Select an location where you want to put the image
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Select();

// Page onto the document. wordApp is the instance of Microsoft.Office.Interop.Word.Application
wordApp.Selection.Paste();

0

我碰巧看到了这篇文章,从资源文件中添加图像非常简单。我找到了一种方法,可以让我在不将其保存到本地机器的情况下工作。

 // PictureContentContorl                        
    Microsoft.Office.Tools.Word.PictureContentControl ct;
    // Removing the control if already exists
                       Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument).Controls.Remove("HeaderLogo");
    // adding control to a range rng
                            ct = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument).Controls.AddPictureContentControl(rng, "HeaderLogo");
                            ct.Image = Properties.Resources.my_logo;
                            ct = null;

也许对某些人有帮助。


0

我陷入了同样的困境,而且我找不到一种直接使用图像资源并且不需要事先保存的方法。

一个很好的文件夹来保存这些文件是 My.Computer.FileSystem.SpecialDirectories.Temp


0

我勉强但成功地完成了:

if (!File.Exists("singleright.png"))
{
object obj = Properties.Resources.singleright;
System.Drawing.Bitmap rs = (System.Drawing.Bitmap)(obj);
rs.Save("singleright.png");
}
insertionPoint.InlineShapes.AddPicture("singleright.png", ref LinkToFile, ref SaveWithDocument);

这确实有效 - 当我发布我的Word-Addin时,它也可以在外国计算机上工作。但我仍然不喜欢它。不应该需要在客户端计算机上保存来自资源的图像文件,而应该能够直接使用资源。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接