0

making panel background color opaque causes slow painting of the controls in the panel.

I have .net 4 winforms app it uses tabs and on 2 of the tabs i load a background image. I overlay this with a panel with controls on it make them opaque by setting

 panel1.BackColor = Color.FromArgb(200, Color.White);

If i comment out the line the panel and controls draw instantly, with the above line it takes a second to paint all the controls.

How can i have it paint faster but still be opaque?

Edit

I have added the following code but still have the same problem:

class DrawPanel : Panel
    {
        public DrawPanel()
        {
            this.DoubleBuffered = true;
        }
    }
Adrian
  • 1,089
  • 24
  • 51
  • Use a __DoubleBuffered__ subclass of Panel : `DrawPanel { public DrawPanel { DoubleBuffered = true;}}` – TaW Jul 05 '16 at 11:41
  • 1
    That's not an opaque color. It's 78% opacity (200/255). How many panels are you rendering this way? – theB Jul 05 '16 at 11:45
  • @ theB 2 panels, one on per tab, 2 tabs. – Adrian Jul 05 '16 at 12:39
  • @TaW I have added code as per my edit but it still is very slow! not sure if i did it right or not! – Adrian Jul 05 '16 at 12:47
  • Well, did you also change the panel1 you use to be a DrawPanel?? You can do that by bravely editing the two spots in the Form1Designe.cs file whe it is declared (at the end) and created (at the top) from `Panel` to `DrawPanel` – TaW Jul 05 '16 at 12:54
  • 1
    You are using a partially transparent color. Something the Panel class supports, however it needs to render the background pixels first before applying the BackColor brush. It asks its Parent to paint those pixels. That of course takes more time. If the parent has a BackgroundImage then it can take a *lot* more time, the usual problem. You must pay attention to the pixel format and the size of a background image to get decent rendering speeds. Use 32bppPArgb, it is x10 times faster than all the other ones. And be sure to resize the image yourself so it is an exact fit. – Hans Passant Jul 05 '16 at 12:54
  • @TaW not sure what i need to change sorry... – Adrian Jul 05 '16 at 13:34
  • @HansPassant this does seem to fit what is happening, I have manually sized the background image to be an exact match to the tab page. however i don't know to use 32bppPArgb to speed things up any examples of how to use it for a tab control? – Adrian Jul 05 '16 at 13:37
  • Use the [Bitmap constructor](http://stackoverflow.com/a/3567824/17034) that takes the PixelFormat argument. – Hans Passant Jul 05 '16 at 13:42

0 Answers0