Texture Alpha

I’m trying to set a certain color of my jpg file to be transparent (the white background) and I’m not able to get it to work.

I’ve tried various permutations of setting the BlendState in the sprite batch begin and various alphas in the color of my spritebatch draw. I can get the image to disappear completely, appear completely or be very bright, like I added a layer of white to it. I’m not able to make only the white background transparent.

I think I may not understand what this operation is really doing to get the desired effect. I did a bunch of googling to no avail. Any suggestions?

I verified that my image actually had a white background by opening it in paintbrush, grabbing a pixel color and seeing that it was 255,255,255.

This is on Windows 10, Visual Studio 2015, MonoGame 3.5

Edit: If I edit the image in GIMP and add an the alpha channel, it works properly with no sprite batch begin settings and using color white in my draw.

The Color Mask tests for an exact colour match. JPEG is a lossy compression scheme, which means you may not get the exact same colour when it is uncompressed. So what you may think is white may be not-quite-white, which will fail the test for the colour mask.

Konaju,

Thanks for the response. I actually considered that scenario which is why I checked the image whites to verify that were actually white and also tried saving as PNG, BMP and JPG. I could get none of the formats to get the white to be transparent.

Anything else I should try?

Post some code so we can see exactly what you’re doing?

Can you not use alpha during image creation?

Something like paint.net can do it easy enough, select all white and press ‘Del’. Save as png. All them deleted areas are now transparent pixels.

See demo code below. This also shows the white background on the ball.

I used this graphic: http://images.clipartpanda.com/sports-balls-clipart-soccer-ball-clip-art-12.png

I noticed under the Pipeline in the Processor tab under the image that
ColorKeyColor: {R:255 G:0 B:255 A:255} ColorKeyEnabled: True
I tried setting color key to false and that didn’t make a difference. The ColorKeyColor doesn’t seem to be editable.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AlphaTest
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D ball;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            ball = Content.Load<Texture2D>("ball");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();
            spriteBatch.Draw(ball, new Vector2(100, 100), Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

I was able to get the image transparency to work on the first shot but I’d also like to understand why this feature isn’t working for me.

Try updating MonoGame, this was broken for a bit on develop, but I think it’s fixed now.

I removed the Monogame reference, went to NuGet and installed 3.5.1.1679, closed and re-opened Visual Studio. The ColorKeyColor is still not editable.

Did I correctly update Monogame? Do I need to update the pipeline tool differently?

I think it is fixed on the develop version only for now.
Go to monogame.net/download/, lower on the page is the link to install the latest develop version.
Or wait for the release of the Stable 3.6