"Parameter is not valid" Arguement Exception when the DoubleBuffered Property is true RRS feed

  • Discussão Geral

  • Hi,

    I am creating a Image button control. So, I have used three images for Up, Down and Over
    events. When I click on the button sometimes, it flickers. I read MSDN and a book where it is said
    that double buffering can stop flickering. So, I set the DoubleBuffered property true. But it causes
    an exception when I try to run the control using UserControlTestContainer.exe program. The
    exception description is given below-

    System.ArgumentException: Parameter is not valid.
       at System.Drawing.Graphics.GetHdc()
       at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
       at System.Drawing.BufferedGraphics.Render()
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.UserControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    The same exception is shown, when I set OptimizedDoubleBuffer option to true -
    this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
    Now, How can I solve this problem? I have to stop image flickering.


    • Tipo Alterado nobugz sábado, 21 de março de 2009 13:19 Resolved
    quinta-feira, 19 de março de 2009 22:21

Todas as Respostas

  • Do you know the line of code where the exception occurs? 
    Mark the best replies as answers. "Fooling computers since 1971."
    sexta-feira, 20 de março de 2009 01:10
  • He doesn't, this is all internal code.  Best thing to do is make a small repro project and post it to a file sharing service.  Like skydrive.live.com
    Hans Passant.
    sexta-feira, 20 de março de 2009 01:56
  • Hi,

    Actually, I don't have a backup of the project source codes that showed the exception.  After submitting this thread, I have made a massive modification in that project source to solve the flickering problem.

    First time, I used transparent background colored Label for Button text and used UserControl.BackgroundImage property to set UP, DOWN, OVER images during each mouse event. But, it showed flickers. So, I tried to enable Double Buffering which in turn caused that exception.

    Instead of solving the problem, I took a different approach to stop flickering. As a replacement for Label,
    BackgroundImage property and Double Buffering, I drew everything, text and image on a bitmap buffer first and then used Graphics.DrawImageUnscaled method to draw the buffered image on the control's surface on each mouse event. This technique stopped the flickering.

    A reply from nobugz, I created almost a similar project like the first one (Caused exception), since I didn't make a backup of my first project. It works same like the previous one but didn't show any exception when the DoubleBuffered property is true. It may happen, because I didn't remember the entire source that I wrote in the first project since I made a massive modification later in that project.

    Anyway, thanks for replying me.


    sábado, 21 de março de 2009 02:00
  • I had the same proble. Not sure why (I must have been tired at the moment - sounds good :=O) ) but I was disposing the graphics object which is wrong because I didn't created it. Check to see if you've done the same. Here's an example:



    protected override void OnPaint(PaintEventArgs e)
    Graphics g = e.Graphics;
    //g.Dispose; //NO!!!

    segunda-feira, 10 de agosto de 2009 15:48
  • Disposing object being incorrect when not creating it was the cause of my head eche with this issue :D
    sexta-feira, 9 de outubro de 2015 22:29
  • Thank you so much! I was about to freak out...but your solution worked just fine!!!
    quarta-feira, 26 de fevereiro de 2020 00:47