来自资源的图像 [英] image from resource
本文介绍了来自资源的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编写此代码以在excel的文件中插入图像:
i write this code for insert an image in excel's file:
// ok lines
Microsoft.Office.Interop.Excel.Application xlApp;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
// line to modify
xlWorkSheet.Shapes.AddPicture(@"C:\Users\fedand\Pictures\TElogoSquare.jpg", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 450, 0, 45, 45);
我想在资源中使用图像。
我必须更改字符串C:\ Users\fedand\Pictures\TElogoSquare.jpg,但是怎么样?
谢谢
I'd like to use an image in resource.
I have to change the string "C:\Users\fedand\Pictures\TElogoSquare.jpg", but how?
thanks
推荐答案
除了解决方案1:
是的,两个AddPicture *方法都需要文件名:
http://msdn.microsoft.com/en-us /library/microsoft.office.interop.excel.shapes.addpicture.ASPX [ ^ ],
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.shapes.addpicture2.ASPX [ ^ ]。
所以,可能你唯一能做的就是将你的资源保存在一个临时文件中,使用这个文件并在以后删除它。首先,要获取资源,请查看自动生成的资源文件(我假设您使用.resx资源)。您最好使用添加现有文件从项目中创建的映像文件创建资源。在自动生成的C#文件中,您将找到类型为Image
的静态属性,其名称与原始文件的名称类似。只是使用它。这是你如何将它保存在一个文件中:
http://msdn.microsoft.com/en-us/library/ktx83wah%28v=vs.110%29.aspx [ ^ ]。
这是获取临时文件的文件名的方法: http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx [ ^ ]。
这就是你删除它的方式: http://msdn.microsoft.com/en-us/library/system.io.file.delete%28v=vs.110%29.aspx [ ^ ]。
或者,考虑使用Open XML SDK而不是Office Interop。以下是Microsoft对使用Interop的警告:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 [ ^ ],
http://support.microsoft.com/kb/257757/en-us [ ^ ]。
请参阅Mika Wendelius的回答: 使用Open XML创建基本的Excel工作簿 [ ^ ]。br />
另见我过去的答案:如何从MS Visual Studio 2010中的添加引用添加microsoft excel 15.0对象库 [ ^ ]。
-SA
In addition to Solution 1:
Yes, both AddPicture* methods required file name:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.shapes.addpicture.ASPX[^],
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.shapes.addpicture2.ASPX[^].
So, probably the only thing you can do is to save your resource in a temporary file, use this file and remove it later. First, to get a resource, look at the auto-generated resource file (I assume you use .resx resource). You should better create a resource from an image file created in your project, using "Add Existing File". In the auto-generated C# file, you will find a static property of the typeImage
with the name similar to the name of the original file. Just use it. This is how you save it in a file:
http://msdn.microsoft.com/en-us/library/ktx83wah%28v=vs.110%29.aspx[^].
This is how you can get a file name for a temporary file: http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx[^].
And this is how you delete it: http://msdn.microsoft.com/en-us/library/system.io.file.delete%28v=vs.110%29.aspx[^].
Alternatively, consider using Open XML SDK instead of Office Interop. Here are the warning from Microsoft against using Interop:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].
Please see this answer by Mika Wendelius: Creating basic Excel workbook with Open XML[^].
See also my past answer: How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^].
—SA
由于 AddPicture
方法需要一个文件名作为第一个参数,因此无法按照要求进行操作。
Since the AddPicture
method requires a filename as the first parameter you cannot do what you are asking.
使用内部资源是不可能的?
我认为我的问题很简单!
It's not possible to use an internal resourse?
I thought that my was a very easy question!
这篇关于来自资源的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文