Transparency and Other Options
When drawing multiple sprites on the screen, you can (and should, for speed
reasons) draw as many of them as possible within one SpriteBatch Begin
and End
block.
In fact, what you'll typically want to do is have one SpriteBatch
object for your game and draw all your 2D images within
that block.
As you saw in Figure 2-4, the XNA logo that I used has a big ugly white background that would probably look better if it were transparent.
There are two ways to render portions of images transparently: either the image file itself must have a transparent background, or the portion of the image that you want to be transparent must be solid magenta (255, 0, 255)—XNA will automatically render solid magenta as transparent. Transparency can be saved to certain file formats (such as .png) that make use of an alpha channel. These formats do not contain only RGB values; instead, each pixel has an additional alpha channel (the A in RGBA) that determines the transparency of the pixel.
If you have an image-editing tool at your disposal, you can use it to create a transparent background on an image of your choice. Paint.net is a great free image-editing tool that is actually written in .NET and is available at http://www.getpaint.net. For the rest of this example I'll be using another XNA logo image file with a transparent background, which is provided with the source code for this section of the book. The file is named logo_trans.png.
Add the transparent XNA logo image to ...
Get Learning XNA 3.0 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.