Yes, you have to dispose them - not just pen and brush, but also Bitmap
and Graphics
.
They don't get disposed when they're out of scope because the variables themselves are references, not objects, and C# compiler doesn't know whether the ownership still belongs to those references or not (e.g. FillEllipse
could, in theory, remember the reference it's given, and try to use it at some later moment - remember that language compiler doesn't have any special knowledge of library semantics!).
If you want to indicate that ownership is restricted to that scope, you use the using
statement:
using (Bitmap bmp = new Bitmap ( 100, 100 ))
using (Graphics g = Graphics.FromImage ( bmp ))
using (Pen p = new Pen ( Color.FromArgb ( 128, Color.Blue ), 1 ))
using (Brush b = new SolidBrush ( Color.FromArgb ( 128, Color.Blue ) ))
{
g.FillEllipse ( b, 0, 0, 99, 99 );
g.FillRegion ( b, pictureBox1.Region );
}
This will make the compiler insert calls to Dispose
automatically as needed, ensuring that all objects are disposed once the corresponding using
scope is left (whether normally, by control transfer such as return
or break
, or an exception).
If you come from a C++ background, using
in C# is directly analogous to a const std::auto_ptr
, except that it's a language construct here, and can only be used for local variables (i.e. not for class fields).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…