Asked by:
"Parameter is not valid" Arguement Exception when the DoubleBuffered Property is true

General discussion
-
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);
Now, How can I solve this problem? I have to stop image flickering.
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
Sincerely,
Lockhid- Changed type nobugz Saturday, March 21, 2009 1:19 PM Resolved
Thursday, March 19, 2009 10:21 PM
All replies
-
Do you know the line of code where the exception occurs?
Mark the best replies as answers. "Fooling computers since 1971."Friday, March 20, 2009 1:10 AM -
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.Friday, March 20, 2009 1:56 AM -
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.
Sincerely,
LockhidSaturday, March 21, 2009 2:00 AM -
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:
#region
OnPaint
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
.
.
.
//g.Dispose; //NO!!!
}
#endregionMonday, August 10, 2009 3:48 PM -
Disposing object being incorrect when not creating it was the cause of my head eche with this issue :DFriday, October 9, 2015 10:29 PM
-
Thank you so much! I was about to freak out...but your solution worked just fine!!!Wednesday, February 26, 2020 12:47 AM