title: C# Transparent Panels Refresh Problems
body: <p>I'm currently working on a project that requires animation on a transparent panel. I have been able to create a transparent panel and draw on it, but when I refresh the panel, where I have drawn with the pen tool is not being redrawn as transparent. This is leaves the last position of what I drew previously stained on the panel.</p>
  
  <p>I assume there is a simple way to override OnPaint or Refresh to redraw all of the pixels on the panel as transparent, but I can't find a solution online or figure it out myself.</p>
  
  <p>Here is the code I used to make the background transparent:</p>
  
  <pre><code>public class TransparentPanel : Panel
  {
      protected override CreateParams CreateParams
      {
          get
          {
              CreateParams cp = base.CreateParams;
              cp.ExStyle |= 0x20;
              return cp;
          }
      }
  
      protected override void OnPaintBackground(PaintEventArgs e)
      {
      }
   }
  </code></pre>
  
  <p>This was my best failed attempt to redraw the background as transparent:</p>
  
  <pre><code>protected override void OnPaint(PaintEventArgs pe)
  {
          pe.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100,255,255,255)), this.ClientRectangle);
  }
  </code></pre>
  
  <p>The problem with this solution is that the background becomes opaque white after a couple refreshes.</p>
  
  <p>Can anyone help me figure out how to do this? I'm super new to graphics and animation, and I assume this has a fairly simple answer. Thanks in advance.</p>
  
  <hr>
  
  <p><em><strong></em>***</strong><em>EDIT</em><strong><em>*</em>**</strong>
  As per Dyppl's response I have changed the way I draw the graphics to the panel. Here is my current code:</p>
  
  <pre><code>    public TransparentPanel fighterPanel;
  ...
      fighterPanel.Paint +=new PaintEventHandler(fighterPanel_Paint);
  ...
      fighterPanel = new TransparentPanel();
      fighterPanel.Location = new System.Drawing.Point(600, 300);
      fighterPanel.Size = new System.Drawing.Size(400, 400);
      gameArenaForm.Controls.Add(fighterPanel);
      fighterPanel.BringToFront();        
  ...
      private void fighterPanel_Paint(object sender, PaintEventArgs e)
      {
          using (Pen blackPen = new Pen(Color.Black, 3), redPen = new Pen(Color.Red, 3), bluePen = new Pen(Color.Blue, 3))
          {
              //head     
              e.Graphics.DrawEllipse
                  (blackPen,
                  head.DrawPoint(torso.GetTorsoAngle(), torso.neck.getLocationX(), torso.neck.getLocationY()).X,
                  head.DrawPoint(torso.GetTorsoAngle(), torso.neck.getLocationX(), torso.neck.getLocationY()).Y,
                  head.radius * 2,
                  head.radius * 2
                  );
              //torso
              e.Graphics.DrawLine(blackPen, torso.neck.getLocationX(), torso.neck.getLocationY(), torso.shoulders.getLocationX(), torso.shoulders.getLocationY());
              e.Graphics.DrawLine(blackPen, torso.shoulders.getLocationX(), torso.shoulders.getLocationY(), torso.hips.getLocationX(), torso.hips.getLocationY());
              //right arm
              e.Graphics.DrawLine(blackPen, torso.shoulders.getLocationX(), torso.shoulders.getLocationY(), rightArm.elbow.getLocationX(), rightArm.elbow.getLocationY());
              e.Graphics.DrawLine(redPen, rightArm.elbow.getLocationX(), rightArm.elbow.getLocationY(), rightArm.attachHand.getLocationX(), rightArm.attachHand.getLocationY());
              //left arm
              e.Graphics.DrawLine(blackPen, torso.shoulders.getLocationX(), torso.shoulders.getLocationY(), leftArm.elbow.getLocationX(), leftArm.elbow.getLocationY());
              e.Graphics.DrawLine(bluePen, leftArm.elbow.getLocationX(), leftArm.elbow.getLocationY(), leftArm.attachHand.getLocationX(), leftArm.attachHand.getLocationY());
              //right leg
              e.Graphics.DrawLine(blackPen, torso.hips.getLocationX(), torso.hips.getLocationY(), rightLeg.knee.getLocationX(), rightLeg.knee.getLocationY());
              e.Graphics.DrawLine(redPen, rightLeg.knee.getLocationX(), rightLeg.knee.getLocationY(), rightLeg.attachFoot.getLocationX(), rightLeg.attachFoot.getLocationY());
              //left leg
              e.Graphics.DrawLine(blackPen, torso.hips.getLocationX(), torso.hips.getLocationY(), leftLeg.knee.getLocationX(), leftLeg.knee.getLocationY());
              e.Graphics.DrawLine(bluePen, leftLeg.knee.getLocationX(), leftLeg.knee.getLocationY(), leftLeg.attachFoot.getLocationX(), leftLeg.attachFoot.getLocationY());
          }
      }
  </code></pre>
  
  <p>Here are some pictures of before and after the object moves and refreshes a few times:</p>
  
  <p><a href="https://docs.google.com/leaf?id=0B0ZghwqWgbQWODljODY1OGQtNzBjYS00ZTA3LWE1MGItNzUyMjIyMWU5NGEz&amp;hl=en_US&amp;authkey=CNGLv_4B" rel="nofollow">Before</a></p>
  
  <p><a href="https://docs.google.com/leaf?id=0B0ZghwqWgbQWYmNjODkzMWMtZGM5OC00ZjRjLThhZjItZGNkMGI0OWZkNjNm&amp;hl=en_US&amp;authkey=CMHX99UF" rel="nofollow">After</a></p>
  
  <p>Sorry, it won't let me embed the images unless I have 10 Rep.</p>