0

I have a png image that has no background, with an alpha channel. Despite pawn.BackColor = Color.Transparent;, the background is still there. What is it about?

Here is my whole code:

///
namespace ludo_game
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            board.ImageLocation = "C:\\..\board.jpg";
            board.SizeMode = PictureBoxSizeMode.StretchImage;
            board.Size = new Size(500, 500);
            List<PictureBox> bluePawns = new List<PictureBox>();
            List<PictureBox> redPawns = new List<PictureBox>();
            List<PictureBox> yellowPawns = new List<PictureBox>();
            List<PictureBox> greenPawns = new List<PictureBox>();
            LoadPawns(bluePawns, "blue", 72, 130, 360, 415);
            LoadPawns(yellowPawns, "yellow", 418, 360, 360, 415);
        }

        private void LoadPawns(List<PictureBox> pawns, string color, int x1, int x2, int y1, int y2)
        {
            for (int i = 0; i < 4; i++)
            {
                PictureBox pawn = new PictureBox();
                pawn.ImageLocation = $"C:\\..\\{color}_pawn.png";
                pawn.SizeMode = PictureBoxSizeMode.StretchImage;
                pawn.Size = new Size(10, 16);
                pawn.BackColor = Color.Transparent;
                pawns.Add(pawn);
                this.Controls.Add(pawn);
                pawn.BringToFront();
            }
            pawns[0].Location = new Point(x1, y1);
            pawns[1].Location = new Point(x2, y1);
            pawns[2].Location = new Point(x1, y2);
            pawns[3].Location = new Point(x2, y2);

        }
    }
}

Here is my sample image of pawn and what it is display:

enter image description hereenter image description here

9
  • Try to set the BackColor of the picture box to Color.Transparent or to the desired background color. Commented Jul 2 at 17:02
  • I've got BackColor set to Color.Transparent. It doesn't work.
    – Layla98
    Commented Jul 2 at 17:07
  • Transparent in the context of Winforms means "Show me the background color of the parent control here". You add the pictureboxes to the form so you will get to see the background color of the form. Presumably you have set the forms background color to white.
    – Ralf
    Commented Jul 2 at 17:23
  • I tried various options with this.TransparencyKey but it still doesn't work... What should I change, what should I add, what should I use?
    – Layla98
    Commented Jul 2 at 17:50
  • What is your expectation? There is no real transparency in Winforms. What you get is that the transparent parts are painted by the parent with its background. Do you want the background of the board control? Then add your pictureboxes to that control. Do you want a complex background? With parts shown from different controls coincidental "behind" your pictureboxes? The later won't work. You can't use stacks of controls then. You need to paint the stuff you want onto a single control and forget about trying to use multiple controls with transparency.
    – Ralf
    Commented Jul 2 at 18:18

0

Browse other questions tagged or ask your own question.