我正在製作一些實用程序類,它們可以將不同類型的符號放置到CAD圖紙的立面上。 我想確保如果我需要處理我這樣做的GraphicsPath對象。如果GraphicsPath在使用後丟棄
在getCircle函數內部的代碼中,它顯示我將myPath「GraphicsPath」對象傳遞給AddStringToPath函數。
我不能使用using(){}作用域,因爲我傳遞了myPath圖形對象作爲參考。
這個設計可以使用嗎?還是我需要去關於這個不同的方式來確保垃圾收集?
GraphicsPath getCircle(Graphics dc, string text = "")
{
GraphicsPath myPath = new GraphicsPath();
myPath.AddEllipse(symbolCircle);
AddStringToPath(dc, ref myPath, text);
return myPath;
}
void AddStringToPath(Graphics dc, ref GraphicsPath path, string text)
{
SizeF textSize = dc.MeasureString(text, elevFont);
var centerX = (path.GetBounds().Width/2) - (textSize.Width/2);
var centerY = (path.GetBounds().Height/2) - (textSize.Height/2);
// Add the string to the path.
path.AddString(text,
elevFont.FontFamily,
(int)elevFont.Style,
elevFont.Size,
new PointF(centerX + 2, centerY + 2),
StringFormat.GenericDefault);
}
謝謝。再加上一個更好的命名約定。 –
哪個更好,'var path ='或'GraphicsPath path ='? – barlop