This blog is no longer updated please visit http://www.createdbyx.com/ instead.

Monday, May 17, 2004

Revisited: How to create a D3DTexture from an embeded image resource.

I have continued to play around with loading embeded resource data files and have come up with a alternative way to create a D3DTexture from an embeded resource image. This new method seems more apropreate. (To me at least :p )

If you are wondering why I am loading embeded resource images rather than simply loading and creating a D3D texture from a file, it is because external files can be lost and can result in you app not being able to run. Not that I will be doing this for every app I write!



Protected Friend Function LoadResource(ByVal ResourceName As String) As IO.MemoryStream
Dim ResourceStream As IO.Stream
ResourceStream = Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(ResourceName)
If ResourceStream Is Nothing Then Return Nothing

Dim byts(CInt(ResourceStream.Length - 1)) As Byte
Dim Len As Integer = ResourceStream.Read(byts, 0, CInt(ResourceStream.Length))

Dim MemStream As New IO.MemoryStream(byts, 0, Len)
Return MemStream
End Function


Protected Friend Function LoadResourceImage(ByVal ResourceImageName As String) As Direct3D.Texture
Dim Mem As IO.MemoryStream
Mem = LoadResource(ResourceImageName)

' attempt to load/create image we saved to memory stream
Dim Tex As Direct3D.Texture
Tex = Direct3D.TextureLoader.FromStream(mobjGraphics.Device, Mem)

' done with varibles
Mem.Close()
Mem = Nothing

' return reference to texture
Return Tex
End Function