Resize and load texture 2d in XNA

I'm new to XNA just in case. What I'm trying to do is load the texture at a different size from its original, or at least be able to resize it afterwards. 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 ignores the ContentManager and that I make it harder to work with the garbage collector.

What is the correct way to upload an image of any size using the ContentManager? If this is not possible, can I resize it proportionally, for example, using scaling?

Context: I create a board nx n. When n is too large, I need to automatically get smaller sizes.

+6
c # xna
source share
2 answers

To load a texture:

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

To resize, use one of the SpriteBatch overloads, which takes a "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 the Education Directory at create.msdn.com

+10
source share
 Texture2D texture; protected override void LoadContent() { ... texture = Content.Load<Texture2D>("Tank"); ... } protected override void Draw(GameTime gameTime) { ... Rectangle destinationRectangle = new Rectangle(100, 100, 30, 10); spriteBatch.Draw(texture, destinationRectangle, Color.White); ... spriteBatch.End(); base.Draw(gameTime); } 
0
source share

All Articles