2011-01-06 113 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; 

可能對某人有幫助。

相關問題