eandr127
eandr127

Reputation: 35

Scaling a Texture2D without Draw()

I am trying to scale a Texture2D without using the Draw() method. The reason being I am not going to be drawing the Texture2D until I perform further manipulations. I would be saving the Texture2D as a field.

Upvotes: 3

Views: 245

Answers (2)

user3135449
user3135449

Reputation: 11

I don't know what kind of image manipulations you want to perform on your image but I highly recommend not performing scaling before you do those manipulations. If there is any way possible to do so, manipulate your image before you scale it. Xna has already taken care of all the dirty work of scaling for you.

If you want to perform specific pixel operations, Texture2D.GetData will work for you but only in small quantities. If you're doing this to hundreds of images, you'll slow down your game drastically. I highly recommend doing some post-processing effects using a customized Effect.

Edit: I just thought of a way to do this the way you want to do it. What you can do is draw your scaled texture to a RenderTarget2D object and then get the color data from it and manipulate the data however you'd like. An example below:

RenderTarget2D renderTarget = new RenderTarget2D(GraphicsDevice, textureWidth, textureHeight, false, GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);

GraphicsDevice.SetRenderTarget(renderTarget);

spriteBatch.Begin();
//scale and draw your texture here
spriteBatch.End();

GraphicsDevice.SetRenderTarget(null);

This draws the texture to the render target which you can then draw later just like you would any other texture:

spriteBatch.Draw(renderTarget, new Rectangle(), Color.White);

You can use renderTarget.GetData to get color data just like you would with Texture2D and manipulate it to your liking.

Upvotes: 1

pinckerman
pinckerman

Reputation: 4213

The first thing I can think is to use Texture2D.GetData and store your texture in an array of Color, uint or whatever, and then perform your scale.
This will require some basic computer graphics knowledge, and I don't think that's the better way to do it.

Upvotes: 0

Related Questions