How to Read an embedded resource as array of bytes without writing it to disk?

if you are reading an embeded resource here is a simple way to do so.

string resourcePath = "pack://application:,,,/resource/location/S_2/{0}";
        StreamResourceInfo resInfo = Application.GetResourceStream(new Uri(resourcePath));

        if (resInfo == null)
        {
            throw new Exception("Resource not found: " + resourcePath);
        }

        var ms = new System.IO.MemoryStream();
        await resInfo.Stream.CopyToAsync(ms);
        byte[] bytes = ms.ToArray();

Keep in mind that Embedded resource filename = Assemblyname.fileName

string fileName = "test.pdf";
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
string fileName = a.GetName().Name + "." + "test.pdf";

using (Stream resFilestream = a.GetManifestResourceStream(fileName))
{
    if (resFilestream == null) return null;
    byte[] ba = new byte[resFilestream.Length];
    resFilestream.Read(ba, 0, ba.Length);
    var byteArray = ba;
}

Simple alternative using a MemoryStream:

var ms = new MemoryStream();
await resFilestream.CopyToAsync(ms);
var bytes = ms.ToArray();

You are actually already reading the stream to a byte array, why not just stop there?

public static byte[] ExtractResource(String filename)
{
    System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
    using (Stream resFilestream = a.GetManifestResourceStream(filename))
    {
        if (resFilestream == null) return null;
        byte[] ba = new byte[resFilestream.Length];
        resFilestream.Read(ba, 0, ba.Length);
        return ba;
    }
}

edit: See comments for a preferable reading pattern.