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

  • 일반 토론

  • 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.


    • 유형 변경됨 nobugz 2009년 3월 21일 토요일 오후 1:19 Resolved
    2009년 3월 19일 목요일 오후 10:21

모든 응답

  • Do you know the line of code where the exception occurs? 
    Mark the best replies as answers. "Fooling computers since 1971."
    2009년 3월 20일 금요일 오전 1: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.
    2009년 3월 20일 금요일 오전 1: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.


    2009년 3월 21일 토요일 오전 2: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!!!

    2009년 8월 10일 월요일 오후 3:48
  • Disposing object being incorrect when not creating it was the cause of my head eche with this issue :D
    2015년 10월 9일 금요일 오후 10:29
  • Thank you so much! I was about to freak out...but your solution worked just fine!!!
    2020년 2월 26일 수요일 오전 12:47