2011-01-06 111 views
2

Interop.Word和C#存在一些图像问题。我想在我要生成的文档的标题中添加一个图像。我有这样的代码工作完美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了未处理。

回答

0

广东话你用...

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驱动器。

我认为它不好的做法,像这样重要的文件无法在应用程序文件夹...

+0

试过它,但无济于事。我还编辑了图像的属性,并将“构建操作”设置为“嵌入资源”和“复制到输出目录”以复制Alway,但仍然没有发生任何事情。 – iamnobody 2011-01-06 19:18:49

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时,也在外国计算机上。但我仍然不喜欢它。不应该有必要从客户端计算机上的资源保存图像文件,应该能够直接使用该资源。

0

我被困在相同的情况,我无法找到一种方式来直接使用图像资源与InlineShapes而不必保存之前。

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

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; 

可能对某人有帮助。

相关问题