# DirectX 9 alternative way to draw image

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

## Recommended Posts

Hello, maybe anybody could help with drawing images on screen?

Now using D3DXCreateTextureFromFileEx: loading LPDIRECT3DTEXTURE9 by file name, width and height. And later using texture with LPD3DXSPRITE.Draw(texture,rect,center,position,color). But if I change rect size, texture don't scale, just stretching in wrong way (https://www.dropbox.com/s/pn6920yar2o4kat/2013-09-25%2018_35_06-APPTITLE.png: normal image, increased width, increased height).

Am I doing something wrong? Maybe my way don't let change width/height after image is loaded? Maybe where is another way to draw images?

	D3DXIMAGE_INFO d3dxImageInfo;
if(FAILED(D3DXCreateTextureFromFileEx(
g_dxManager.GetDevice(),
fileName.c_str(),
width,
height,
1,
D3DPOOL_DEFAULT,
D3DFMT_UNKNOWN,
D3DPOOL_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DCOLOR_COLORVALUE(1.0f,0.0f,1.0f,1.0f), //ignores 255;0;255 - dark pink
&d3dxImageInfo,
NULL,
&_texture)))
{
}


and drawing code:

			_sprite->Draw( sprite->Texture()->Texture(),//LPDIRECT3DTEXTURE9 is loaded from method above
&rect,
&vCenter,
&vPosition,
D3DCOLOR_COLORVALUE(
sprite->Texture()->TextureColor()._r,
sprite->Texture()->TextureColor()._g,
sprite->Texture()->TextureColor()._b,
sprite->Texture()->TextureColor()._a));


Edited by Plutonium

##### Share on other sites

Hey I'm not seeing any of the images you mentioned. Generally speaking though, if you increase the size of the rect, the image will just stretch across the new surface since the tex-coords haven't changed.

##### Share on other sites

rect is maybe lager than you think? Are the textures witdth/height a power of 2 numbers?

Anyway this is caused by the sampler UV address mode. Evrything is explained here:

http://msdn.microsoft.com/en-us/library/windows/desktop/bb206239(v=vs.85).aspx

Edited by imoogiBG

##### Share on other sites
This is indeed what is happening: ID3DXSprite::Begin sets the addressing mode to clamp. One could change this after Begin or use D3DXSPRITE_DONOTMODIFY_RENDERSTATE, but I don't recommend the latter since you would need to set all the necessary states for proper sprite rendering by hand now.

The culprit is elsewhere. Note that the rectangle parameter in ID3DXSprite::Draw is the source rectangle, i.e. you select a part from the texture. Set pSrcRect to NULL to select the whole texture.

If you want to scale (or arbitrarily transform) a sprite you need to use ID3DXSprite::SetTransform before calling Draw. Unfortunately it isn't as simple as telling a target rectangle.

##### Share on other sites

This is indeed what is happening: ID3DXSprite::Begin sets the addressing mode to clamp. One could change this after Begin or use D3DXSPRITE_DONOTMODIFY_RENDERSTATE, but I don't recommend the latter since you would need to set all the necessary states for proper sprite rendering by hand now.

The culprit is elsewhere. Note that the rectangle parameter in ID3DXSprite::Draw is the source rectangle, i.e. you select a part from the texture. Set pSrcRect to NULL to select the whole texture.

If you want to scale (or arbitrarily transform) a sprite you need to use ID3DXSprite::SetTransform before calling Draw. Unfortunately it isn't as simple as telling a target rectangle.

If setting renderstate, then nothing is rendered to screen, so as you recommended, I won't change it.

Whole idea is that I could increase only one object width/height, while others will be as was. This is for my map builder, that could put objects and change their size and position, then save to xml. Yep, I could leave it now as it's, because for editing will be ok, but thinking about future :)

This one implemented, but it changes all images on screen, without changing real width/height. So... Thanks, now I have zooming/rotation functions :D

• 18
• 18
• 11
• 21
• 16