Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2006

Tip: Looking for answers? Try searching our database.

Caching a Graphics object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nickdu - 07 Feb 2006 21:09 GMT
Someone suggested that calling Control.CreateGraphics() is expensive and thus
it might be a good idea to cache the returned Graphics object.  Is this true?

Back in the unmanaged world I remember something like there were only 5 DC's
to work with within your application so if objects attempted to cache their
DC's or if you had a leak your application would quickly freeze.  I assume
the same sort of thing applies with the Graphics object unless it's a layer
above the DC.  But if it's a layer above the DC I would not expect creating
one to be an expensive operation.  Also, if it is expensive to create a
Graphics object and there is no harm in caching one I would have expected the
underlying framework (Windows / .NET) to have done the caching for me instead
of having each user of the framework implement their own caching mechanism.

(Also posted on the .NET drawing newsgroup)
Signature

Thanks,
Nick

Herfried K. Wagner [MVP] - 07 Feb 2006 21:31 GMT
"nickdu" <nickdu@discussions.microsoft.com> schrieb:
> Someone suggested that calling Control.CreateGraphics() is expensive and
> thus
> it might be a good idea to cache the returned Graphics object.  Is this
> true?

No.  'Graphics' objects should not be cached because they will get invalid
if the underlying window handle gets recreated.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>

nickdu - 08 Feb 2006 13:19 GMT
So they shouldn't be cached just because of recreating the window, or as I
mention below, does it hold onto a scarce resource (DC?)?  I think someone
indicated a third party control library which caches the Graphics object and
re-caches (lets go of the current one and caches a new one) it each time the
window is resized.  Someone else pointed me to:

http://msdn.microsoft.com/chats/transcripts/vstudio/vstudio_062602.aspx

which contains:

PeterG_MS
Q: Regarding overhead of passing objects to event handlers (such as
Graphics). There's more overhead than passing a pointer. The object must be
created and initialized. For every event, that seems like it would be a lot
of overhead. No?

PeterG_MS
A: Creating a Graphics object would have some significant overhead -- I
don't really know how much. But just passing a Graphics object has little
cost. So one way to improve performance would be a cache a Graphics object
and pass the same way on each event invocation, if possible.

I would rather stay away from caching the graphics object if possible.  
Again my assumption is that if it's expensive to create the graphics object
and there is no harm in caching it the underlying framework (Windows / .NET)
would have cached it for us.

Signature

Thanks,
Nick

> "nickdu" <nickdu@discussions.microsoft.com> schrieb:
> > Someone suggested that calling Control.CreateGraphics() is expensive and
[quoted text clipped - 4 lines]
> No.  'Graphics' objects should not be cached because they will get invalid
> if the underlying window handle gets recreated.
Inayat Azmi - 09 Feb 2006 17:22 GMT
Hi Nickdu:
All graphics objects are created based on current graphics context of the
control so please drop the ideas of caching them.

> Someone suggested that calling Control.CreateGraphics() is expensive and thus
> it might be a good idea to cache the returned Graphics object.  Is this true?
[quoted text clipped - 10 lines]
>
> (Also posted on the .NET drawing newsgroup)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.