[Solved]Embedding a Texture2D in a .DLL

Related to this topic here.

I’m trying to embed a Texture2D into a .DLL, then load it from the assembly, but I’ve been having some problems. I suspect that I’m going about this horribly wrong. Here’s some of the code:

First, I generate a file like this:

Texture2D write = Content.Load< Texture2D >(“font 12”);
StreamWriter writer = new StreamWriter(“font 12”);
byte array = new byte[write.Height * write.Width];
write.GetData< byte >(array);
for (int i = 0; i < array.Length; i++)

The idea being that I’m writing a byte array to a file from the Texture2D data.

Then I add the generated file to the DLL project as an embedded file. When it’s time to load the file, I use this code:

Stream stream = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream(“GUI.font 12”);
Texture2D Font = new Texture2D(device, 430, 12);
List< byte > data = new List< byte >();
for (int i = 0; i < stream.Length; i++)
Font.SetData< byte >(data.ToArray());

Then the project I’m using the .DLL in builds, but it doesn’t display properly. The line

Stream stream = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream(“GUI.font 12”);

might be part of the problem, since I honestly have no idea what Type to pass to GetAssembly(). “This” is just a class from my .DLL.

Am I doing this horribly wrong? My other idea was to embed the .xnb file itself in the .DLL and then generate it when the .DLL is used, but I ran into different troubles there.

You should add the xnb file as a resource in the DLL. Then use ResourceContentManager to access it. See the XNA documentation for an example of using ResourceContentManager.

Also some more information here on the difference between embedding as a resx (as used by ResourceContentManager) and embedding as a resource.

I appreciate the responses. For some reason, I can’t find ResourceContentManager under the Microsoft.Xna.Framework.Content namespace, and typing in ResourceContentManager and going to quick actions doesn’t list any namespace to add. Is it because the .DLL I’m working on wasn’t started as a Monogame project?

Edit: Yep, that’s why. In a Monogame project, I can create a ResourceContentManager, but not in a .DLL project that references Monogame. Is there a way to specifically create a Monogame .DLL project?

Edit again: I started a new Monogame project and then went to project properties and changed the output type to class library. That should do the trick, I think. I’ll post again when I get it working or run into another wall.

Yes! It’s working! Thanks for the help.