什么可能导致双缓冲杀死我的应用程序?

Tom*_*ght 8 c# components gdi+ double-buffering

我有一些使用GDI +绘制到屏幕的自定义(winforms)组件.

为了防止重绘时出现闪烁,我决定启用双缓冲,所以我在构造函数中添加了一行:

public ColourWheel()
{
    InitializeComponent();
    this.DoubleBuffered = true;
}
Run Code Online (Sandbox Code Playgroud)

哪个适用于此组件(ColourWheel).当我将相同的行添加到我的另外两个(结构相似的)组件的构造函数中时,我会得到一些奇怪的症状:

  1. 当我尝试运行组件打开的表单时,我得到一个Argument Exception Application.Run(new Form());.
  2. 如果我切换到设计模式,我得到一个错误,关于具有未处理异常的组件与参数.

我是否对其中一个或全部进行双缓冲似乎并不重要,它仍然适用于ColourWheel,但不适用于其他.

为了记录,我还尝试了一些其他的 缓冲技术.

什么可能导致双缓冲在一个组件上工作,而不是其他组件?


编辑:这是运行时症状的异常细节:

System.ArgumentException未处理Message = Parameter无效.Source = System.Drawing StackTrace:System.Drawing.Graphics.GetHdc(),位于System.Windows.Forms.Control的System.Drawing.BufferedGraphics.Render()的System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC,BufferedGraphics缓冲区)中.系统中System.Windows.Forms.UserControl.WndProc(Message&m)的System.Windows.Forms.ScrollableControl.WndProc(Message&m)处的System.Windows.Forms.Control.WndProc(Message&m)处的.WmPaint(Message&m) .Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam, IntPtr lparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)处于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)at at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason,ApplicationContext context),位于TestForm.Program.Main()的System.Windows.Forms.Application.Run(Form mainForm)的System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)中D:\ Documents and Settings\Tom Wright\My Documents\Visual Studio 2010\Projects\ColourPicker\TestForm\Program.cs:在System.AppDomain.ExecuteAssembly的System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)的第18行(String assemblyFile,Evidence assemblySecurity,String [] args)在System.Threading.ExecutionContext.Run的System.Threading.ThreadHelper.ThreadStart_Context(Object state)中的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()(ExecutionContext executionContext,ContextCallback callback) System.Threading.ThreadHelper.ThreadStart()InnerException上的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态),对象状态,布尔值ignoreSyncCtx):


编辑2:导致问题的两个组件中的一个(更复杂)的OnPaint处理程序:

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);
               }
           }
        }
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*igt 10

你不应该GraphicsPaint活动期间处理借给你的物品,这就是你的using块不正确地做了什么.

症状是下次Paint事件触发时,您Graphics返回相同的对象,但它不再绑定到内存中HDC,导致Graphics.GetHdc()失败,如堆栈跟踪中所示.

  1. 它有可能超过单个Paint事件(这很可能是双缓冲的情况,尽管如果CS_OWNDC设置了窗口样式,也可以使用单缓冲).

  2. Paint事件可以有多个处理程序.

因此,事件处理程序不应调用DisposeGraphics对象或允许using块这样做.相反,.NET框架会在Paint事件处理完成后根据需要清理资源.

  • @yano:或者当然,这只意味着您创建的那些。毁坏借来的物品对真正的主人来说是非常不友善的。 (3认同)