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

Sunday, May 16, 2004

How to create a D3DTexture from an embeded image resource.

I have added a cool little code snip that demonstrates a way of loading a image that was added to a project and declared "Embeded Resource" at design time, and returns a newly created D3D texture.
The code is written in vb.net and DX9.

Load Resource Texture
Private Function LoadResourceImage(ByVal ResourceImageName As String) As Direct3D.Texture

Dim Mem As New IO.MemoryStream()
Dim Img As Bitmap

' attempt to create bitmap from embeded resource
Img = New Bitmap(Me.GetType, ResourceImageName)
' save image to memory stream
Img.Save(Mem, Drawing.Imaging.ImageFormat.Png)
' seek to start of stream
Mem.Seek(0, IO.SeekOrigin.Begin)

' 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
Img.Dispose()
Img = Nothing

' return reference to texture
Return Tex
End Function