To create a texture from a PNG file, use the Texture2D.FromStream()( MSDN ) method .
To draw different parts of a texture, use the sourceRectangleoverload option SpriteBatch.Drawthat accepts it ( MSDN ).
Here is a sample code:
using(FileStream stream = File.OpenRead("uploaded.png"))
{
myTexture = Texture2D.FromStream(GraphicsDevice, stream);
}
spriteBatch.Begin();
spriteBatch.Draw(myTexture, new Vector2(111), new Rectangle( 0, 0, 50, 50), Color.White);
spriteBatch.Draw(myTexture, new Vector2(222), new Rectangle( 0, 50, 50, 50), Color.White);
spriteBatch.Draw(myTexture, new Vector2(333), new Rectangle(50, 0, 50, 50), Color.White);
spriteBatch.Draw(myTexture, new Vector2(444), new Rectangle(50, 50, 50, 50), Color.White);
spriteBatch.End();
source
share