1

A beginner question... but despite studying I'm not sure about how System.Drawing.Graphics works. Does it always need presence of using statement even if e.Graphics was supplied to me? I already know about Q/A When do I need to use dispose on Graphics.

In other words: in the following example, can C#/VB Using statement be safely omitted?

Private Sub MyControl_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    Using e.Graphics
        e.Graphics.DrawRectangle(...)
        ControlPaint.DrawFocusRectangle(e.Graphics, ...)
    End Using
End Sub
Community
  • 1
  • 1
miroxlav
  • 11,796
  • 5
  • 58
  • 99
  • @Sinatr That question is slightly unrelated. This is more related: http://stackoverflow.com/q/4316149/613130 and the answer is no, because you didn't create it. – xanatos Jun 24 '15 at 09:51
  • 2
    It's worth highlighting the comment below the accepted answer to that question: "do not dispose an object you didn't create. Disposing e.Graphics is a nasty latent bug" – Damien_The_Unbeliever Jun 24 '15 at 09:54
  • @usr – question clarified - please review. Now duplicate question is not closely related. – miroxlav Jun 24 '15 at 09:57
  • 3
    @miroxlav The answer is clearly written in is comment (that is a direct quote from Hans: *do not dispose an object you didn't create. Disposing e.Graphics is a nasty latent bug*). So the answer is "the `using` *must* be removed" – xanatos Jun 24 '15 at 10:00
  • @xanatos – yes :) so either let's change duplicate link to that answer or I can delete the entire question. May I ask you what do you recommend? – miroxlav Jun 24 '15 at 10:01

0 Answers0