源代码如下,不停的在窗体上绘制一条曲线,没绘制一次内存使用大概增多10K,有没有优化的办法啊?
gdi绘图中必须有事件paint吗?如果没有该事件那如何把bmp上的东西绘制到用户界面上呢?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace CurveDemo
{
    public partial class Demo : Form
    {
        private Color gridColor = Color.DarkGreen;
        private int gridWidth = 10;
        private int gridHeight = 10;
        private Pen gridPen;
        Bitmap bmp = null;
        Point[] points;
        public Demo()
        {
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            InitializeComponent();
        }
               private void Demo_Load(object sender, EventArgs e)
        {
            gridPen = new Pen(gridColor);
            bmp = new Bitmap(this.Width, this.Height);
            Graphics g = Graphics.FromImage(bmp);            for (float i = this.Height; i > 0; i = i - gridHeight)
            {
                g.DrawLine(gridPen, 0, i, this.Width, i);
            }
            for (float j = this.Width; j > 0; j = j - gridWidth)
            {
                g.DrawLine(gridPen, j, 0, j, this.Height);
            }
            int ii = 2;
            Point p1 = new Point(5 + ii, 5 * ii);
            Point p2 = new Point(10 * ii, 0);
            Point p3 = new Point(15 * ii, 15 * ii);
            Point p4 = new Point(20 * ii, 0);
            Point p5 = new Point(25 * ii, 25 * ii);
            points = new Point[] { p1, p2, p3, p4, p5 };
            g.DrawCurve(Pens.Red, points);
            
            gridPen.Dispose();
            g.Dispose();
        }        private void Demo_Paint(object sender, PaintEventArgs e)
        {
            
            Graphics dc = e.Graphics;
            dc.FillRectangle(Brushes.Black, this.ClientRectangle);
            if (bmp != null)
            {
                dc.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
            }
        }
        int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            
            i++;
            Point p1 = new Point(5+i, 5*i);
            Point p2 = new Point(10*i, 0);
            Point p3 = new Point(15*i, 15*i);
            Point p4 = new Point(20*i, 0);
            Point p5 = new Point(25*i, 25*i);
            points = new Point[] { p1, p2, p3, p4, p5 };
            draw();
            Refresh();
        }
        private void draw()
        {
            Graphics g = Graphics.FromImage(bmp);
            g.DrawCurve(Pens.Red,points);
            g.Dispose();
            
        }
  
    }
}

解决方案 »

  1.   

    谢谢您,真的很感谢,我测试的时候没有测试到内存使用达到6M的情况,呵呵,谢谢您,我想再请教您个问题:
    平时我们在绘图的时候都是在Paint事件中进行绘图的(也就是进行一系列的绘图操作),即使不在paint事件中绘图也是在一个绘图面上把所有的绘图操作完成之后再在paint中把绘制好的绘图面利用paint中的Graphics dc=e.Graphics;中的dc来绘制到用户界面上(dc.DrawImage(bmp)),类似于我上面提问的那个问题中那样,但在ZedGraph中包括在使用该控件时根本没有 paint事件,那绘图面是如何画到用户界面上的呢?
      

  2.   

    在Timer_tick里面Point 不要每次都去new,用属性来修改
      

  3.   

    说错了,不是属性,是索引,points数组不要每次都new
      

  4.   

    恩 good idea,谢谢提醒。
      

  5.   

    不知道是不是重写了OnPaint实现的。囧
    Point,每次new并不是大问题。毕竟是值类型的。变量的生命周期短,创建速度快(在栈上分配)。
      

  6.   

    Paint事件只会在界面重绘的时候才会触发,刚开始的时候设备并不会关心是否存在Paint事件。