Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat could cause Double Buffering to kill my app?
    primarykey
    data
    text
    <p>I have a few custom (winforms) components that draw to the screen using GDI+.</p> <p>To prevent flickering on repaint, I decided to enable double buffering, so I added a line to my constructor:</p> <pre><code>public ColourWheel() { InitializeComponent(); this.DoubleBuffered = true; } </code></pre> <p>Which works fine on this component (ColourWheel). When I add the same line to the constructor of either of my other two (similarly structured) components, I get a couple of strange symptoms:</p> <ol> <li>When I try to run a form with the component on, I get an Argument Exception on <code>Application.Run(new Form());</code>.</li> <li>If I switch to design mode, I get an error about the component having an unhandled exception to do with a parameter.</li> </ol> <p>It doesn't seem to matter whether I turn double buffering on one or all of them, it still works on the ColourWheel, but not the others.</p> <p><em>For the record, I've also tried a few other <a href="https://stackoverflow.com/a/77233/50151">double</a> <a href="https://stackoverflow.com/a/181384/50151">buffering</a> techniques.</em></p> <p>What could be causing double buffering to work on one component, but not others?</p> <hr> <p><strong>EDIT:</strong> Here's the exception detail from the run-time symptom:</p> <blockquote> <p>System.ArgumentException was unhandled Message=Parameter is not valid. Source=System.Drawing StackTrace: 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&amp; m) at System.Windows.Forms.Control.WndProc(Message&amp; m) at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m) at System.Windows.Forms.UserControl.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at TestForm.Program.Main() in D:\Documents and Settings\Tom Wright\My Documents\Visual Studio 2010\Projects\ColourPicker\TestForm\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:</p> </blockquote> <hr> <p><strong>EDIT 2:</strong> The OnPaint handler from one (the more complicated) of the two components that are causing problems:</p> <pre><code>private void ValueSlider_Paint(object sender, PaintEventArgs e) { using (Graphics g = e.Graphics) { g.DrawImage(this.gradientImage, new Rectangle(0, 0, paintArea.Width, paintArea.Height)); if (this.showmarker) { ColourHandler.HSV alt = ColourHandler.RGBtoHSV(new ColourHandler.RGB(this.SelectedColour.R, this.SelectedColour.G, this.SelectedColour.B)); alt.Saturation = 0; alt.value = 255 - alt.value; using (Pen pen = new Pen(ColourHandler.HSVtoColour(alt))) { pen.Width = (float)MARKERWIDTH; g.DrawRectangle(pen, 0 - pen.Width, this.brightnessPoint.Y - MARKERWIDTH, this.paintArea.Width + (pen.Width * 2), MARKERWIDTH * 2); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload