Search code examples
c#embedded-resource

How to get resource as byte array in c#?


I added image to my c# project from Project settings -> Resources
How can i get this image at runtime?
I trying this:

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

I call this function this way:

byte[] data = GetResource("TestImg.png");

But I see my image in Resources folder in project explorer.
enter image description here

Could anyone tell what's wrong there?
enter image description here


Solution

  • You need to set the file TestImg.png as an "Embedded Resource." The resource name would then be Resources/TestImg.png.