Reputation: 308
How do you center an image inside a rectangle with:
Batch.draw(imagename, new Rectangle(x, y, imagename.width, imagename.height), Color.White);
Upvotes: 0
Views: 2401
Reputation: 14153
You can use the origin overload for spritebatch
Batch.draw(imagename, new Rectangle(x, y, imagename.width, imagename.height), Color.White,0f,new Vector2(imagename.Width /2, imagename.Height /2) ,SpriteEffects.Null, 0);
Upvotes: 1
Reputation: 17520
One of the overloads for SpriteBatch.Draw()
asks for a Texture2D
(the image), a Vector2
(the position of the image), and a Color
(the tint of the image). You should not have to use a rectangle for drawing your image. Just use some math to get the correct X
and Y
values for the position.
You will have to take into consideration the height/width of the image, and the height/width of the containing object (like the screen or something).
Another option is to use an overload that takes the Origin
parameter. Set this to the relative center of your image. This will change the "draw origin" of the image from the top-left to the center.
Upvotes: 0