c# - Create XNA sprite on the fly from an image -
i have image, let's says .png, uploaded user. image has fixed size, let's 100x100.
i create 4 sprites image.
one (0,0) (50,50)
another (50, 0) (100, 50)
the third (0, 50) (50, 100)
the last (50, 50) (100, 100)
how can prefered c# ?
thanks in advance help
to create texture png file, use texture2d.fromstream()
method (msdn).
to draw different sections of texture, use sourcerectangle
parameter overload of spritebatch.draw
accepts (msdn).
here's example code:
// presumably in update or loadcontent: using(filestream stream = file.openread("uploaded.png")) { mytexture = texture2d.fromstream(graphicsdevice, stream); } // in draw: 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();
Comments
Post a Comment