Reputation: 5907
I need to import jpeg images into a WP7/XNA app with associated metadata. The program which manages these images exports to an XML file with an encoded byte[] of the jpg files.
I've written a custom importer/processor which successfully imports the reserialized objects into my XNA project.
My question is, given the byte[] of the jpg, what is the best way to convert it back to Texture2D.
// 'Standard' method for importing image
Texture2D texture1 = Content.Load<Texture2D>("artwork"); // Uses the standard Content processor "Texture - XNA Framework" to import an image.
// 'Custom' method
var myCustomObject = Content.Load<CompiledBNBImage>("gamedata"); // Uses my custom content Processor to return POCO "CompiledBNBImage"
byte[] myJPEGByteArray = myCustomObject.Image; // byte[] of jpeg
Texture2D texture2 = ???? // What is the best way to convert myJPEGByteArray to a Texture2D?
Thanks very much for your help. :-)
DS
Upvotes: 1
Views: 1601
Reputation: 449
To answer the first part of your question, you would create an instance of Texture2D and then fill it with color information via the SetData() method. Just be sure the dimensions are correct in the constructor.
Texture2D tex = new Texture2D(graphics, 100, 100);
tex.SetData(byteArray);
The second part and potentially the tricky part will be ensuring the byte array is in the right format for the SetData() method, although give it a try; it might just work in the current format. :)
Upvotes: 1