Sign in to follow this  
Yours3!f

Convert HBITMAP to LPDIRECT3DTEXTURE9

Recommended Posts

Yours3!f    1532
Hi,

I want to convert a HBITMAP loaded from a resource file to a LPDIRECT3DTEXTURE9.
This is how far I got:

[code]
int load_bitmap(HBITMAP bitmap)
{
BITMAP bmp; //bitmap for getting the raw data
GetObject(bitmap, sizeof(bmp), &bmp);
int size = bmp.bmWidth * bmp.bmHeight * (bmp.bmBitsPixel / 8); //width * height * 24bits / 8
BYTE* bytes = new BYTE[size]; //container for storing temporary raw data
GetBitmapBits(bitmap, size, bytes); //getting the raw data

for(int c = 0; c < (bmp.bmWidth * bmp.bmHeight); c++) //go through the pixels
{
BYTE tmp = bytes[c * 3 + 0]; //swap red and blue channels, so we get RGB from BGR
bytes[c * 3 + 0] = bytes[c * 3 + 2];
bytes[c * 3 + 2] = tmp;
}

//create the texture
HRESULT result = D3DXCreateTextureFromFileInMemoryEx(dx_dev, //the dirextx device
bytes,
size,
bmp.bmWidth,
bmp.bmHeight,
D3DX_DEFAULT,
0,
D3DFMT_R8G8B8,
D3DPOOL_MANAGED,
D3DX_DEFAULT,
D3DX_DEFAULT,
0,
NULL,
NULL,
&texture); //texture is a LPDIRECT3DTEXTURE9 object

//check the result
if(result == D3DERR_NOTAVAILABLE)
{
return -1;
}
else if(result == D3DERR_OUTOFVIDEOMEMORY)
{
return -1;
}
else if(result == D3DERR_INVALIDCALL)
{
return -1;
}
else if(result == D3DXERR_INVALIDDATA)
{
return -1; //I always get this
}
else if(result == E_OUTOFMEMORY)
{
return -1;
}

dx_dev->SetTexture(0, texture);

if(texture != NULL)
{
return 0;
}
else
{
return -1;
}
}
[/code]

I always get the invalid data error, what do I do wrong?

Best regards,
Yours3!f

Share this post


Link to post
Share on other sites
Evil Steve    2017
[font="Arial"]D3DXCreateTextureFromFileInMemoryEx expects the data you provide it to be a .bmp file loaded into memory, with the various headers and stuff on it, whereas you're just passing it raw pixel data. If you want to do that, you need to create the texture, then use D3DXLoadSurfaceFromMemory() on the top surface level, and then call GenerateMipSubLevels() on the texture.

However, it makes more sense to just use D3DXCreateTextureFromResourceEx().
[/font]

Share this post


Link to post
Share on other sites
Yours3!f    1532
[quote name='Evil Steve' timestamp='1310389561' post='4833710']
[font="Arial"]D3DXCreateTextureFromFileInMemoryEx expects the data you provide it to be a .bmp file loaded into memory, with the various headers and stuff on it, whereas you're just passing it raw pixel data. If you want to do that, you need to create the texture, then use D3DXLoadSurfaceFromMemory() on the top surface level, and then call GenerateMipSubLevels() on the texture.

However, it makes more sense to just use D3DXCreateTextureFromResourceEx().
[/font]
[/quote]

Thank you, I wasn't aware of the [font="Arial"]D3DXCreateTextureFromResourceEx() function, this is why I wanted to load directly raw data.[/font] But I guess [font="Arial"]D3DXCreateTextureFromResourceEx() will be just enough :)[/font]

Share this post


Link to post
Share on other sites
Yours3!f    1532
I can't get it working :(

I tried to replace the HRESULT result = ... with this:

[code]
HRESULT result = D3DXCreateTextureFromResource(dx_dev,
(HMODULE)bitmap,
NULL,
&texture);
[/code]

but I get the same error...

Share this post


Link to post
Share on other sites
mhagain    13430
Your second param should normally be NULL, and your third should be MAKEINTRESOURCE (ResourceID), where ResourceID is the numeric ID associated with the resource in your .rc file (or it's #define).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this