picturebox transparency malfunction

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EntryTeam
    New Member
    • Aug 2009
    • 55

    picturebox transparency malfunction

    I'm writing turn based logic game, and transparency doesn't seen to work right:
    screenshot

    Person model is .GIF in picturebox, buildings are .GIF in pictureboxes, mainform background is .bmp file.

    Model picbox and building picboxes both have
    Code:
    .BackColor = System.Drawing.Color.Transparent;
    ...set.

    How do I get this working as it should be?
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You don't. It is the nature of a picturebox backgroundcolor to show through to the background color of the control underneath it.

    if you put a picturebox on a form whose background color is red, then your picturebox background color is red.

    A Form's background color of transparent will work as you are expecting. So instead of making a bunch of pictureboxes on a form you can make a bunch of forms with no border and transparent background and a background image.

    This is why it is good to do small-scale experiment to prove one's assumptions before doing the BIG part of the coding.

    Comment

    • EntryTeam
      New Member
      • Aug 2009
      • 55

      #3
      Originally posted by tlhintoq
      you can make a bunch of forms with no border and transparent background and a background image
      You mean real forms instead of pictureboxes? How's that?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by tlhintoq
        A Form's background color of transparent will work as you are expecting. So instead of making a bunch of pictureboxes on a form you can make a bunch of forms with no border and transparent background and a background image.
        .
        Can you get away with using a panel instead of a Form?
        Transparent forms has been a big mess (if you look through post history here in bytes, there's been a few long threads about getting it to work)

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          A panel might work. I don't think I've tried for a purpose such as EntryTeam is doing.

          Sounds like a good experiment.

          EntryTeam: Give it a try. Instead of a bunch of picturebox controls, try it with a bunch of panel controls. They have nearly all the same properties such as 'Image', 'Backcolor', 'Location' etc. so it shouldn't be a huge issue.

          If you are creating those pictureboxes programatically you just make a new panel instead of new picturebox
          Code:
                      PictureBox pbPlayer = new PictureBox();
                      // becomes
                      Panel pbPlayer = new Panel();
          If you built them at design time, you can go into the Form1.Designer. cs file and change their type.

          Code:
                  #region Windows Form Designer generated code
          
                  /// <summary>
                  /// Required method for Designer support - do not modify
                  /// the contents of this method with the code editor.
                  /// </summary>
                  private void InitializeComponent()
                  {
                      this.button1 = new System.Windows.Forms.Button();
                      this.button2 = new System.Windows.Forms.Button();
                      this.[I][B]pictureBox1[/B][/I] = new System.Windows.Forms.[I][B]PictureBox[/B][/I]();
                      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
                      this.SuspendLayout();
          
                      // 
                      // button1
                      // 
                      this.button1.Location = new System.Drawing.Point(26, 13);
                      this.button1.Name = "button1";
                      this.button1.Size = new System.Drawing.Size(75, 23);
                      this.button1.TabIndex = 1;
                      this.button1.Text = "button1";
                      this.button1.UseVisualStyleBackColor = true;
                      this.button1.Click += new System.EventHandler(this.button1_Click);
                      // 
                      // button2
                      // 
                      this.button2.Location = new System.Drawing.Point(207, 12);
                      this.button2.Name = "button2";
                      this.button2.Size = new System.Drawing.Size(75, 23);
                      this.button2.TabIndex = 2;
                      this.button2.Text = "button2";
                      this.button2.UseVisualStyleBackColor = true;
                      this.button2.Click += new System.EventHandler(this.button2_Click);
                      // 
                      // pictureBox1
                      // 
                      this.[B][I]pictureBox1[/I][/B].Location = new System.Drawing.Point(3, 3);
                      this.[I][B]pictureBox1[/B][/I].Name = "pictureBox1";
                      this.[B][I]pictureBox1[/I][/B].Size = new System.Drawing.Size(100, 50);
                      this.[B][I]pictureBox1[/I][/B].TabIndex = 0;
                      this.[B][I]pictureBox1[/I][/B].TabStop = false;
                      // 
                      // Form1
                      // 
                      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                      this.ClientSize = new System.Drawing.Size(544, 497);
                      this.Controls.Add(this.button2);
                      this.Controls.Add(this.button1);
                      this.Name = "Form1";
                      this.Text = "Form1";
                      ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
                      this.ResumeLayout(false);
          
                  }
          
                  #endregion
          
                  private System.Windows.Forms.Button button1;
                  private System.Windows.Forms.Button button2;
                  private System.Windows.Forms.[I][B]PictureBox[/B][/I] pictureBox1;
          
              }
          This way you don't have to delete controls and make new ones, then attach event handles and so on - you just change the type of the existing control.

          Comment

          • EntryTeam
            New Member
            • Aug 2009
            • 55

            #6
            2 Plater
            2 tlhintoq
            I'll rewrite now picturebox code. It's good thing that I have all the controls dynamic.

            Comment

            • EntryTeam
              New Member
              • Aug 2009
              • 55

              #7
              No, panel doesn't work... The same problem.

              Comment

              • rashipurohit
                New Member
                • Mar 2010
                • 9

                #8
                Hiii friends i have same problem. If anybody have any idea then please reply me. it is very argent.


                Thanks in advance

                Comment

                Working...