Resize and Load a texture2d in XNA
Asked Answered
B

2

7

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.

Bunyabunya answered 3/12, 2010 at 20:31 Comment(1)
Joe's answer is correct. Additionally: there is no difference to the garbage collector whether you use ContentManager or not. It only affects how you must unload the textures, if that is something your game requires (eg: when changing between levels). Take a look at my answer here: #4265495.Hundred
H
12

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

Haff answered 3/12, 2010 at 20:38 Comment(1)
thank you!, it works just in the way i want :)...Now about the question if use or not the methods that i mention, what do you think is safe, or his use is other thing?Bunyabunya
O
1
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);
        }
Octastyle answered 18/1, 2017 at 22:10 Comment(1)
Welcome to Stack Overflow! Code-only answers are not very helpful. Please edit your answer to explain why your code solves the original problem.Coryza

© 2022 - 2024 — McMap. All rights reserved.