gpt4 book ai didi

C# Interop.Word 从项目资源文件夹添加图像

转载 作者:太空宇宙 更新时间:2023-11-03 14:23:54 25 4
gpt4 key购买 nike

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

最佳答案

你不能用...

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

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

关于C# Interop.Word 从项目资源文件夹添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4617395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com