1

I'm trying to get an image in the resources as a byte[] for insertion into a database. The resource is at Resources/CatSeal and is a file called index.jpg.

I've looked at this question, but I'm still having trouble. I'm getting a NullReferenceException on the indicated line. My namespace is DatabaseConnectionTests. According to this documentation, under "Access Resources" it should follow this format, which I believe I'm doing:

MyNameSpace.MyImage.bmp

Here's my code:

Stream sourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("DatabaseConnectionTests.index.jpg");
using (var memoryStream = new MemoryStream())
{
    sourceStream.CopyTo(memoryStream); // NullReferenceException here
    seal.SealerImage = memoryStream.ToArray();
}
sealDatabaseOperations.Insert(seal);

How can I resolve this so that my resource image is loaded to a byte[]? Thanks in advance.

Community
  • 1
  • 1
Scotty H
  • 6,432
  • 6
  • 41
  • 94

1 Answers1

2

Looks like it's not finding your resource.

Try: "DatabaseConnectionTests.Resources.index.jpg"

Set a breakpoint in a class in the same assembly and evaluate this:

this.GetType().Assembly.GetManifestResourceNames()

That will list all resource names avail for that assembly.

CainBot
  • 434
  • 2
  • 8
  • Ok, thanks, so running that in the debugger returns "DatabaseConnectionTests.Seals.resources", but using "DatabaseConnectionTests.Seals.resources.index.jpg" in `GetManifestResourceStream` still produces a NullReferenceException. Any ideas? – Scotty H Sep 16 '15 at 13:52
  • Hi, can I just check that you've changed the jpg files Build Action to Embedded Resource? – CainBot Sep 16 '15 at 21:39