
public void drawWord_first(object sender, PaintEventArgs e)
{
Graphics gd = e.Graphics;
//g.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath stringPath = new GraphicsPath();
charInfo chars=new charInfo();
string drawWords = chars.getChars();
stringPath.AddString(chars.getChars(), new FontFamily("楷體_GB2312"), 0, 96, new Point(0, 0), StringFormat.GenericDefault);
gd.DrawPath(new Pen(Brushes.Blue),stringPath);
gd.Dispose();
}

public void OnPaint_drawTian(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
this.tianHeight = 200;
this.tianWidth = 200;
this.tianLeftTop = new Point(0,0);
this.tianRightTop = new Point(this.tianWidth,0);
this.tianLeftBottom = new Point(0,this.tianHeight);
this.tianRightBottom = new Point(this.tianWidth,this.tianHeight);
this.tianTopCenter = new Point(this.tianWidth/2,0);
this.tianBottomCenter=new Point(this.tianWidth/2,this.tianHeight);
this.tianLeftCenter = new Point(0,this.tianHeight/2);
this.tianRightCenter = new Point(this.tianWidth,this.tianHeight/2);
this.tianRectangle = new Rectangle(this.tianLeftTop,new Size(this.tianWidth,this.tianHeight));
this.tianPen=new Pen(Brushes.Red,2);
this.tianPen.DashStyle = DashStyle.DashDot;
g.DrawLine(this.tianPen, this.tianRectangle.Left, this.tianRectangle.Top, this.tianRectangle.Left + this.tianWidth, this.tianRectangle.Bottom);
g.DrawLine(this.tianPen,this.tianRightTop,this.tianLeftBottom);
g.DrawLine(this.tianPen,this.tianLeftCenter,this.tianRightCenter);
g.DrawLine(this.tianPen,this.tianTopCenter,this.tianBottomCenter);
this.tianPen.DashStyle = DashStyle.Solid;
g.DrawRectangle(this.tianPen,this.tianRectangle);
//g.Dispose();
}
這樣釋放內存會報 參數無效 異常,經過思索,貌似第一個g對象被釋放掉以后,第二個繪制就會成問題,所以正確寫法應當如下:
public void OnPaint_drawTian(object sender, PaintEventArgs e){...//g.Dispose();}
public void drawWord_first(object sender, PaintEventArgs e){...g.Dispose();}
請問大家,這樣寫對不,兩個函數緊挨着被調用(注釋掉第一個dispose語句就是了)
另外內存的釋放順序應該與使用的順序相反...
-------------------------------------------------------------------------------------------------------------------
Dispose與Close沒有任何關系.它們的共同點是遵循一個相似的約定.
Dispose的約定是,這個方法將釋放該實例所占用的所有資源,銷毀自身.
注意Dispose的約定是釋放所有資源並銷毀自身,就是說大多數情況下,被Dispose掉的實例都不應該被再次使用.
如果一個類同時擁有Dispose和Close方法,一般來說,Dispose中都會調用Close方法.
Close 只是關閉而已,沒有釋放真正的釋放資源,可以重新打開
Dispose 是把對象銷毀,將不再存在
Close是停業整頓,Dispose是炸毀;
Close是關門,Dispose是破產。