Jump to content
  • Advertisement
Sign in to follow this  
Yours3!f

Convert HBITMAP to LPDIRECT3DTEXTURE9

This topic is 2596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

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


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;
}
}


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
Advertisement
[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

[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]


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
I can't get it working :(

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


HRESULT result = D3DXCreateTextureFromResource(dx_dev,
(HMODULE)bitmap,
NULL,
&texture);


but I get the same error...

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!