1 public Ex_Panel() 2 { 3 InitializeComponent(); 4 this.SetStyle(ControlStyles.OptimizedDoubleBuffer | 5 ControlStyles.ResizeRedraw | 6 ControlStyles.AllPaintingInWmPaint, true); 7 } 8 9 10 Bitmap desBmp = (Bitmap)global::Custom.Controls.Properties.Resources.logicback; 11 private void Ex_Panel_Paint(object sender, PaintEventArgs e) 12 { 13 Rectangle rect = e.ClipRectangle; 14 BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current; 15 BufferedGraphics myBuffer = currentContext.Allocate(e.Graphics, e.ClipRectangle); 16 Graphics g = myBuffer.Graphics; 17 g.SmoothingMode = SmoothingMode.HighQuality; 18 g.PixelOffsetMode = PixelOffsetMode.HighSpeed; 19 g.Clear(this.BackColor); 20 using (TextureBrush Txbrus = new TextureBrush(desBmp)) 21 g.FillRectangle(Txbrus, rect); 22 myBuffer.Render(e.Graphics); 23 g.Dispose(); 24 myBuffer.Dispose();//释放资源 25 }
以上是我的代码,在重绘以后达到了双缓冲的效果,提高了绘图效果,但是在动态增加空间时出现了一定的问题,currentContext.Allocate(e.Graphics, e.ClipRectangle);这个函数出现了内存溢出的情况。有那位遇到过这个问题。望赐教。
目前已经解决;增加API函数。减少背景绘制。详细见我的博客。