How to fit a sprite into a fixed size rectangle?

If you have an image, you can draw a sprite with that image in a specific rectangle size. For instance, I have a menu background image of 480 x 320 pixels and the Windows Phone 7 screen is 480 x 800, if I draw it without specifying a sized rectangle, only the texture, position and color it would look like this:
_
ScreenManager.SpriteBatch.Draw(mMenuBackgroundSprite.Texture, mMenuBackgroundObject.Position, Color.White);

Original Size
Where:
  • mMenuBackgroundSprite.Texture is a Texture2D initialized with the image.
  • mMenuBackgroundObject,Position is the position where that 2d object should appear in the WP7 screen, in this case is a Vector2D initialized as (0,0).
  • Color.White indicates that is going to be drawn in full color without tint.
But if I specify a rectangle to fit the sprite like this:
_
ScreenManager.SpriteBatch.Draw(mMenuBackgroundSprite.Texture, new Rectangle(0,0, 800, 480), Color.White);

It would look like this:

Original Size
Where “new Rectangle(0, 0, 800, 480)” defines a rectangle positioned in the coordinates x=0, y=0, with a size of 800 x 480 pixels to fit the whole screen.
_
*Made on XNA 4.0 with C# 2010 – Microsoft Visual Studio 2010 Express for Windows Phone.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s