View Code
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();
}
View Code
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是破產。