如何在c#中将资源作为字节数组?

Kos*_*mos 2 c# embedded-resource

我从项目设置 - >资源
中将图像添加到我的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;
}
Run Code Online (Sandbox Code Playgroud)

我这样称呼这个函数:

byte[] data = GetResource("TestImg.png");
Run Code Online (Sandbox Code Playgroud)

但我在项目浏览器的Resources文件夹中看到了我的图像.
在此输入图像描述

有谁能说出那里有什么问题?
在此输入图像描述

Col*_*son 7

您需要将文件设置TestImg.png为"嵌入式资源".那么资源名称就是Resources/TestImg.png.