Sign in to follow this  
Zelzer

Spline positioning problem after scaling

Recommended Posts

Hi all.

I have a control (picturebox), in which I want to draw 2D images (splines) at different positions (some kind of VNC - updating different rectangles) with the help of DirectX.

When I receive the first image, I must resize it to fit picturebox' size, and than resize other images with this scale. I've written the following code to complete it:

[code]

void InitializeGraphics()
{
// Set our presentation parameters
var presentParams = new PresentParameters() ;
presentParams.SwapEffect = SwapEffect.Discard;
presentParams.Windowed = true;

// Create our device
_device = new Device(0, DeviceType.Hardware, pictureBox1,
CreateFlags.SoftwareVertexProcessing, presentParams);
_sprite = new Sprite(_device);
}


Matrix GetTransformationMatrix(float width, float height)
{
var scaleWidth = _currentWidthScale = pictureBox1.Width / width;
var scaleHeight = _currentHeightScale = pictureBox1.Height / height;
return Matrix.Scaling(scaleWidth, scaleHeight,1.0F );
}


Texture LoadTexture(string path)
{
return TextureLoader.FromFile(_device, path);
}


public void Draw()
{
using(var texture = LoadTexture())
{
_device.BeginScene();
_sprite.Begin(SpriteFlags.None);

Rectangle textureSize;
using (var s = texture.GetSurfaceLevel(0))
{
SurfaceDescription desc = s.Description;
textureSize = new Rectangle(0, 0, desc.Width, desc.Height);
}
if(_first)
{
_sprite.Transform = GetTransformationMatrix(textureSize.Width, textureSize.Height);
}
Vector3 position = new Vector3(_currentPositionX, _currentPositionY, 1F);

_sprite.Draw(texture, textureSize, _center, position, Color.White);
_sprite.End();
_device.EndScene();
_device.Present();
}
}
}
[/code]




Scaling works fine. The problem is with the position . For example, when the first image is 1280x800, and I try to place the small second scaled image on {currentPositionX=640,currentPositionY=400} , it doesn't locate correctly and draw it on some another place. I've spent an hours thinking about this behavior but still have no decision. When scaling is off, everything locates fine.

Any help greatly appreciate.

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