简体   繁体   中英

WP7 C# XNA resize sprite

I have a PNG image much larger than the size I want it to be drawn. I also have a rectangle that I want the PNG fill. I have tried this code but it doesn't resize the PNG:

public void Draw(SpriteBatch batch, float screenWidth)
{
    Rectangle destinationRectangle = new Rectangle(0, 0, 0, 0);
    destinationRectangle.Width = (int)(screenWidth / 8);
    destinationRectangle.Height = (int)(screenWidth / 8);
    Vector2 topLeft = new Vector2(0, 0);
    batch.Begin();
    batch.Draw(GamePage.theImage, topLeft, destinationRectangle, Color.Transparent);
    batch.End();
}

Thanks

Let's imagine your image is 250×250 pixels and you want to fill a 300×300 px rectangle with it. To do that you use a Destination Reactangle with the size of 300×300:

spriteBatch.Draw(yourImage, new Rectangle(12, 34, 300, 300), Color.White);

12 and 34 are the X and Y coordinates of your rectangle.

There is no mentioning of the original size of your image in code, because it doesn't matter, since the program will fill the Destination Rectangle with any given texture.


I'm confused with Color.Transparent in your code, do you really intend to draw an invisible sprite?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM