Microsoft DirectX 9.0

D3DXCreateTextureFromFile Function

Creates a texture from a file.


HRESULT D3DXCreateTextureFromFile(      

    LPDIRECT3DDEVICE9 pDevice,     LPCTSTR pSrcFile,     LPDIRECT3DTEXTURE9 *ppTexture );


[in] Pointer to an IDirect3DDevice9 interface, representing the device to be associated with the texture.
[in] Pointer to a string that specifies the filename. If the compiler settings require Unicode, the data type LPCTSTR resolves to LPCWSTR. Otherwise, the string data type resolves to LPCSTR. See Remarks.
[out] Address of a pointer to an IDirect3DTexture9 interface, representing the created texture object.

Return Value

If the function succeeds, the return value is D3D_OK.

If the function fails, the return value can be one of the following values.

D3DERR_NOTAVAILABLEThis device does not support the queried technique.
D3DERR_OUTOFVIDEOMEMORYMicrosoft?Direct3D?does not have enough display memory to perform the operation.
D3DERR_INVALIDCALLThe method call is invalid. For example, a method's parameter may have an invalid value.
D3DXERR_INVALIDDATAThe data is invalid.
E_OUTOFMEMORYDirect3D could not allocate sufficient memory to complete the call.


The compiler setting also determines the function version. If Unicode is defined, the function call resolves to D3DXCreateTextureFromFileW. Otherwise, the function call resolves to D3DXCreateTextureFromFileA because ANSI strings are being used.

This function supports the following file formats: .bmp, .dds, .dib, .jpg, .png, and .tga.

The function is equivalent to D3DXCreateTextureFromFileExW(pDevice, pSrcFile, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, ppTexture).

Mipmapped textures automatically have each level filled with the loaded texture.

When loading images into mipmapped textures, some devices are unable to go to a 1x1 image and this function will fail. If this happens, the images need to be loaded manually.

Note that a resource created with this function will be placed in the memory class denoted by D3DPOOL_MANAGED.

For the best performance when using D3DXCreateTextureFromFile:

  1. Doing image scaling and format conversion at load time can be slow. Store images in the format and resolution they will be used. If the target hardware requires power of two dimensions, create and store images using power of two dimensions.
  2. Consider using Microsoft DirectDraw?surface (DDS) files. Because DDS files can be used to represent any Microsoft DirectX?9.0 texture format, they are very easy for Direct3D extensions (D3DX) to read. Also, they can store mipmaps, so any mipmap-generation algorithms can be used to author the images.

Function Information

Import libraryd3dx9.lib
Minimum operating systems Windows 98

See Also

D3DXCreateTextureFromFileEx, Texture Color Conversions

© 2002 Microsoft Corporation. All rights reserved.