用Drawing画图如何不会消失

方法一:将事件放在form_Load中,在窗体中画图
 
   1:          protected void MainForm_Load(object sender,EventArgs e)
   2:          {
   3:              InitialPoint();        
   4:              Bitmap bm = new Bitmap(this.Width,this.Height);
   5:              Graphics grp = Graphics.FromImage(bm);
   6:              DrawCurves(grp,lpt);
   7:              pictureBox1.Image =bm;
   8:              //if use load event,then use bitmap,if not ,there is no need.
   9:   
  10:          }

方法二:利用picturebox画图,放在Load事件中。

   1:          protected void MainForm_Load(object sender,EventArgs e)
   2:          {
   3:              InitialPoint();        
   4:              Bitmap bm = new Bitmap(pictureBox1.Width,pictureBox1.Height);
   5:              pictureBox1.Image =bm;
   6:              using(Graphics grp =Graphics.FromImage(pictureBox1.Image))
   7:              {
   8:                  DrawCurves(grp,lpt);
   9:              }
  10:   
  11:          }

方法三:将事件放在Paint事件中,这个只能放在form中,因为grp本例和e无关,但是参数是:PaintEventArgs e

   1:          private void Form1_Paint(object sender, PaintEventArgs e)
   2:          {
   3:              //if in this method,there is no need to use bitmap
   4:              InitialPoint();    
   5:              Graphics grp =this.CreateGraphics();
   6:              DrawCurves(grp,lpt);        
   7:          }


方法四:paint事件,在picturebox上,按照下边这个例子,用e这个参数,不仅可以放在form的paint上,也可以放在picturebox的paint上

 
 

   1:          private void pic_paint(object sender,PaintEventArgs e)
   2:          {
   3:               Graphics g = e.Graphics;
   4:               DrawCurves(g,lpt);
   5:          }

   1:          private void DrawCurves(Graphics grp, List<Point> pointList)
   2:          {
   3:              Point[] temps = new Point[pointList.Count];
   4:              pointList.CopyTo(temps);
   5:              grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
   6:              grp.DrawCurve(new Pen(Color.Red, 2), temps);
   7:             // grp.Dispose();如果用Paint事件绘图,这个似乎不可以
   8:          }

注明:lpt是一个List<Point>

上一篇:VC++使用WebBrowser控件,强制给控件指定版本显示网页


下一篇:ThinkPHP -- magic_quotes_gpc()引起的反斜杠问题