문의자
"Parameter is not valid" Arguement Exception when the DoubleBuffered Property is true

일반 토론
-
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- 유형 변경됨 nobugz 2009년 3월 21일 토요일 오후 1:19 Resolved
2009년 3월 19일 목요일 오후 10:21
모든 응답
-
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,
Lockhid2009년 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:
#region
OnPaint
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
.
.
.
//g.Dispose; //NO!!!
}
#endregion2009년 8월 10일 월요일 오후 3:48