Click here to Skip to main content
15,896,154 members
Home / Discussions / C#
   

C#

 
AnswerRe: problem with attaching an existing project to another project Pin
prasadbuddhika16-May-10 22:02
prasadbuddhika16-May-10 22:02 
QuestionSo I Tried Graphics Tonight For The First Time Pin
Roger Wright14-May-10 19:42
professionalRoger Wright14-May-10 19:42 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev14-May-10 20:09
protectorAspDotNetDev14-May-10 20:09 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev14-May-10 20:14
protectorAspDotNetDev14-May-10 20:14 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
Som Shekhar14-May-10 20:30
Som Shekhar14-May-10 20:30 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev14-May-10 21:01
protectorAspDotNetDev14-May-10 21:01 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Pete O'Hanlon14-May-10 22:00
mvePete O'Hanlon14-May-10 22:00 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
OriginalGriff14-May-10 22:22
mveOriginalGriff14-May-10 22:22 
This gets a little complex, but: The Graphics object is a limited resource, which contains unmanaged memory. What that means is that it uses memory which is not on the normal C# heap, or the stack - it is in a different area completely, and the .NET garbage collector will not process it. When your method ends, the last reference to the object is destroyed, so it becomes available to be garbage collected - which is fine. But, the GC will only try to dispose of it when it runs short of managed memory - which could be in a few weeks time, you just don't know! In the meantime, all that unused, unmanaged memory is sitting there, being wasted.
If you manually call Dispose() on your object when you are finished with it, the resources are released immediately, and all is well.

The easiest way to do this is to wrap you object in a using block:
using(Graphics g = CreateGraphics())
   {
   ... use the Graphics object
   }
The system will then call Dispose at the end.

I probably haven't explained this too well, but if you look in any reasonable book on C#, it will talk about it in loads more detail!

BTW: I wouldn't do the drawing in the Button.Click event - do it in the Form.Paint event - which supplies you with a graphics context in e.Graphics - and use the Button.Click to set any parameters (such as where you want it, what color, etc.) then call Invalidate() to force a re-draw.
I have learnt that you can not make someone love you, all you can do is stalk them and hope they panic and give in.

Apathy Error: Don't bother striking any key.

GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Som Shekhar14-May-10 22:41
Som Shekhar14-May-10 22:41 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
OriginalGriff14-May-10 23:40
mveOriginalGriff14-May-10 23:40 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Som Shekhar15-May-10 0:22
Som Shekhar15-May-10 0:22 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Pete O'Hanlon15-May-10 5:55
mvePete O'Hanlon15-May-10 5:55 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
PIEBALDconsult15-May-10 5:00
mvePIEBALDconsult15-May-10 5:00 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev17-May-10 17:21
protectorAspDotNetDev17-May-10 17:21 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
OriginalGriff17-May-10 21:26
mveOriginalGriff17-May-10 21:26 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
AspDotNetDev17-May-10 21:33
protectorAspDotNetDev17-May-10 21:33 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
OriginalGriff17-May-10 21:49
mveOriginalGriff17-May-10 21:49 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
DaveyM6914-May-10 23:18
professionalDaveyM6914-May-10 23:18 
AnswerRe: So I Tried Graphics Tonight For The First Time Pin
Luc Pattyn15-May-10 2:18
sitebuilderLuc Pattyn15-May-10 2:18 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Roger Wright15-May-10 4:46
professionalRoger Wright15-May-10 4:46 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Luc Pattyn15-May-10 5:24
sitebuilderLuc Pattyn15-May-10 5:24 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Som Shekhar15-May-10 5:04
Som Shekhar15-May-10 5:04 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Luc Pattyn15-May-10 5:28
sitebuilderLuc Pattyn15-May-10 5:28 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Roger Wright15-May-10 6:10
professionalRoger Wright15-May-10 6:10 
GeneralRe: So I Tried Graphics Tonight For The First Time Pin
Pete O'Hanlon15-May-10 5:57
mvePete O'Hanlon15-May-10 5:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.