我从项目设置中添加了图像到我的c#项目 - >资源
如何在运行时获取此图像?
我想这一点:如何获取资源在c#中的字节数组?
public byte[] GetResource(string ResourceName)
{
System.Reflection.Assembly asm = Assembly.GetEntryAssembly();
// list all resources in assembly - for test
string[] names = asm.GetManifestResourceNames(); //even here my TestImg.png is not presented
System.IO.Stream stream = asm.GetManifestResourceStream(ResourceName); //this return null of course
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);
return data;
}
我调用这个函数是这样的:
byte[] data = GetResource("TestImg.png");
但我看到我的资源文件夹图像在Project Explorer中。
有人能说出那里有什么问题吗?
为什么你投'stream.Length'到'int'?它已经是'int'了。 '.LongLength'返回一个'长'。 –
我不知道如何,但对我来说Stream.Length是** long ** – Kosmos