【问题标题】:C# Interop.Word Adding an Image from project resource folderC# Interop.Word 从项目资源文件夹添加图像
【发布时间】:2011-01-06 16:51:48
【问题描述】:

Interop.Word 和 C# 存在一些图像问题。我想在要生成的文档的标题中添加一个图像。我的这段代码运行良好

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 未处理。

【问题讨论】:

    标签: c# image ms-word com-interop


    【解决方案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 驱动器。

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

    【讨论】:

    • 已经尝试过,但无济于事。我还编辑了图像的属性并将构建操作设置为嵌入资源并将复制到输出目录设置为始终复制,但仍然没有发生任何事情。
    • 你在那里检查过吗?通过执行类似 Console.out.println(File.Exists("logo.jpg")); ?
    • 它返回假。但是该文件已经在我的 resource.resx 和我的资源文件夹中。我现在真的很困惑。如何解决这个问题?顺便说一句,感谢库鲁的时间。 :)
    • 您应该查看 {project folder}/bin 并查看是否可以在那里找到您的图像。如果图像的“复制到输出目录”设置为“如果较新则复制”,我应该在某个地方
    • 它在 /bin/Debug/Resources 和 /bin/Release/Resources 下
    【解决方案2】:

    我不情愿地——但成功地——做了:

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

    【讨论】:

      【解决方案3】:

      我陷入了同样的境地,我找不到直接使用 InlineShapes 的图像资源而无需之前保存的方法。

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

      【讨论】:

        【解决方案4】:

        我碰巧看到了这篇文章,从资源文件中添加图像是直截了当的。找到了一种方法并为我工作,而无需将其保存到本地机器。

         // 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;
        

        可能对某人有帮助。

        【讨论】:

          【解决方案5】:

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

          // 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();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-09-15
            • 2013-03-12
            • 2022-08-23
            • 1970-01-01
            • 2011-01-25
            • 2011-01-03
            • 1970-01-01
            • 2015-09-12
            相关资源
            最近更新 更多