Resize and Load a texture2d in XNA

mjsr picture mjsr · Dec 3, 2010 · Viewed 23.2k times · Source

i'm a newbie in XNA just in case. What i try to do is load a texture in a different size from his original, or at least have the possibility to change his size after. I see in some places that i can use:

Texture2D.FromStream(GraphicsDevice graphicsDevice, Stream stream, 
                 int width, int height, bool zoom)

But i also read that loading textures in this way is ignoring the ContentManager, and that i'm making the job for the garbage collector more difficult.

What is the Correct way to load an image in any size, using the ContentManager ? If that isn't possible can i change his size proportionally, like using a zoom?

Context: I'm creating a board of n x n peaces. When n is too big i need that automatically the peaces becomes more smaller.

Answer

user60456 picture user60456 · Dec 3, 2010

To load the texture:

Texture2D tex = Content.Load<Texture2D>("somefile");

To resize it use one of the SpriteBatch overloads that takes "scale" http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.draw.aspx

float scale = .5f; //50% smaller
SpriteBatch.Draw(tex, position, source, Color.White, rotation, scale, SpriteEffects.None, 0f);

If you are new to XNA, I suggest you read this short tutorial, and also check out Education Catalog at create.msdn.com