473,383 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Is this structure OK: Me.CreateGraphics.DrawImage(Img, 0, 0)



I did the following and then wondered it's good practice.

Me.CreateGraphics.DrawImage(Img, 0, 0)

I'm thinking about disposing the Graphics object.

A couple of lines after this I exit the sub so I wonder if it'll get
disposed automatically?

Should the above structure be avoided?

In general, if an object is such that it should be disposed and the
reference goes out of scope, say by a sub ending, is it necessary to
explicitly dispose the object?

I had once believed that to be true, now I'm wondering if I should be
reviewing all my code to see if I left any thing undisposed that I shouldn't
have.

Thanks


Mar 22 '07 #1
6 2476
" active" <ac**********@a-znet.comschrieb:
I did the following and then wondered it's good practice.

Me.CreateGraphics.DrawImage(Img, 0, 0)
No, it isn't.
I'm thinking about disposing the Graphics object.

A couple of lines after this I exit the sub so I wonder if it'll get
disposed automatically?

Should the above structure be avoided?
In which context are you making the call to 'CreateGraphics'? Inside
'OnPaint' and 'Paint' event handlers, you can access a 'Graphics' object via
'e.Graphics'.
In general, if an object is such that it should be disposed and the
reference goes out of scope, say by a sub ending, is it necessary to
explicitly dispose the object?
It's not mandatory, but I would consider it a good practice.

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

Mar 22 '07 #2
On Mar 22, 9:10 am, " active" <activeNOS...@a-znet.comwrote:
I did the following and then wondered it's good practice.

Me.CreateGraphics.DrawImage(Img, 0, 0)

I'm thinking about disposing the Graphics object.

A couple of lines after this I exit the sub so I wonder if it'll get
disposed automatically?

Should the above structure be avoided?

In general, if an object is such that it should be disposed and the
reference goes out of scope, say by a sub ending, is it necessary to
explicitly dispose the object?

I had once believed that to be true, now I'm wondering if I should be
reviewing all my code to see if I left any thing undisposed that I shouldn't
have.

Thanks
That is not good practice. Painting should be done in the paint event
where it belongs. See this article:

http://www.bobpowell.net/creategraphics.htm

Also, check out his entire site.

Chris

Mar 22 '07 #3
active wrote:
I did the following and then wondered it's good practice.

Me.CreateGraphics.DrawImage(Img, 0, 0)

I'm thinking about disposing the Graphics object.
You are right in your concerns about the disposing. You should
definitely keep a reference to the object so that you can dispose it:

Dim g As Graphics = Me.CreateGraphics
g.DrawImage(Img, 0, 0)
g.Dispose
A couple of lines after this I exit the sub so I wonder if it'll get
disposed automatically?
When there is no reference to the object any more, it's up for garbage
collection. However, as you haven't disposed the object, it's still in
the finalizer queue. This means that the garbage collector can not free
the object, but instead have to move it to the next heap generation and
wait for a background thread to finalize it. After finalizing, the
garbage collector can free the object.

If you call the Dispose method, the object will be removed from the
finalizer queue. This means that the garbage collector can free the
object at the next garbage collection.

Also, the graphics object contains a handle to a windows graphics object
which you should release by calling Dispose. Otherwise the object will
hold on to the handle until it's finalized, and you risk to run out of
windows resources.
Should the above structure be avoided?
Definitely.
In general, if an object is such that it should be disposed and the
reference goes out of scope, say by a sub ending, is it necessary to
explicitly dispose the object?
Not strictly neccesary, as the finalizer will eventually do that, but
your program will use less resources and put less stress on the memory
management if you do.
I had once believed that to be true, now I'm wondering if I should be
reviewing all my code to see if I left any thing undisposed that I shouldn't
have.
If you have relied on the finalizer to dispose your objects in the past,
I think that it's a good idea to review the code.

--
Göran Andersson
_____
http://www.guffa.com
Mar 22 '07 #4

"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:OH****************@TK2MSFTNGP04.phx.gbl...
>" active" <ac**********@a-znet.comschrieb:
>I did the following and then wondered it's good practice.

Me.CreateGraphics.DrawImage(Img, 0, 0)

No, it isn't.
>I'm thinking about disposing the Graphics object.

A couple of lines after this I exit the sub so I wonder if it'll get
disposed automatically?

Should the above structure be avoided?

In which context are you making the call to 'CreateGraphics'? Inside
'OnPaint' and 'Paint' event handlers, you can access a 'Graphics' object
via 'e.Graphics'.
>In general, if an object is such that it should be disposed and the
reference goes out of scope, say by a sub ending, is it necessary to
explicitly dispose the object?

It's not mandatory, but I would consider it a good practice.

I'll make sure I do it

Thanks
>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 22 '07 #5

"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
On Mar 22, 9:10 am, " active" <activeNOS...@a-znet.comwrote:
>I did the following and then wondered it's good practice.

Me.CreateGraphics.DrawImage(Img, 0, 0)

I'm thinking about disposing the Graphics object.

A couple of lines after this I exit the sub so I wonder if it'll get
disposed automatically?

Should the above structure be avoided?

In general, if an object is such that it should be disposed and the
reference goes out of scope, say by a sub ending, is it necessary to
explicitly dispose the object?

I had once believed that to be true, now I'm wondering if I should be
reviewing all my code to see if I left any thing undisposed that I
shouldn't
have.

Thanks

That is not good practice. Painting should be done in the paint event
where it belongs. See this article:

http://www.bobpowell.net/creategraphics.htm

Also, check out his entire site.

Chris

thanks , I will
>

Mar 22 '07 #6

"Göran Andersson" <gu***@guffa.comwrote in message
news:u5*************@TK2MSFTNGP04.phx.gbl...
active wrote:
>I did the following and then wondered it's good practice.

Me.CreateGraphics.DrawImage(Img, 0, 0)

I'm thinking about disposing the Graphics object.

You are right in your concerns about the disposing. You should definitely
keep a reference to the object so that you can dispose it:

Dim g As Graphics = Me.CreateGraphics
g.DrawImage(Img, 0, 0)
g.Dispose
>A couple of lines after this I exit the sub so I wonder if it'll get
disposed automatically?

When there is no reference to the object any more, it's up for garbage
collection. However, as you haven't disposed the object, it's still in the
finalizer queue. This means that the garbage collector can not free the
object, but instead have to move it to the next heap generation and wait
for a background thread to finalize it. After finalizing, the garbage
collector can free the object.

If you call the Dispose method, the object will be removed from the
finalizer queue. This means that the garbage collector can free the object
at the next garbage collection.

Also, the graphics object contains a handle to a windows graphics object
which you should release by calling Dispose. Otherwise the object will
hold on to the handle until it's finalized, and you risk to run out of
windows resources.
>Should the above structure be avoided?

Definitely.
>In general, if an object is such that it should be disposed and the
reference goes out of scope, say by a sub ending, is it necessary to
explicitly dispose the object?

Not strictly neccesary, as the finalizer will eventually do that, but your
program will use less resources and put less stress on the memory
management if you do.
>I had once believed that to be true, now I'm wondering if I should be
reviewing all my code to see if I left any thing undisposed that I
shouldn't have.

If you have relied on the finalizer to dispose your objects in the past, I
think that it's a good idea to review the code.

--
Göran Andersson
_____
http://www.guffa.com
Thanks for taking so much time to explain

Mar 22 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: news.skynet.be | last post by:
Hello. I am a beginner at this Java stuff, so bear with me. I have been writing an imaging application as a way of learning Java. I have got little bits working and tied them together in an...
2
by: ragha | last post by:
Dear friends I am emulating thr tree structure mentioned in the article http://www.15seconds.com/issue/010921.htm I have succesfully created the folder structure for level 2 I need this...
21
by: Atanas Boev | last post by:
Is there any CSS way to make a <hr /> noshadow so I don't get the default 3D look in Netscape? I tried to use some border-bottom to make the line, but my "line" should be under text that is...
7
by: WALDO | last post by:
I have an interesting quandary. I have developed a program in VB.Net to extract an icon resource from an exe/dll or from an ico file and enumerate its formats (16x16,256 color;32x32,true...
6
by: mphanke | last post by:
Hi, I'm desperately trying to print a string on top of an image, but for whatever reason the images allways stays on top of my canvas! Is there some trick how to implement things? I call ...
1
by: Aravind | last post by:
Hi, I am trying to create a image object out of a bitmap file like Image img = new Bitmap(@"c-\CurDisplayImage.bmp"); ,I found the memory of the process is increased twice as the bitmap size ,...
0
by: Steven | last post by:
I need to draw to an image after it has been rotated, but if I draw something before the rotation is done then DrawImage doesn't work: Here's the code. using System; using System.Drawing; ...
3
by: wildc | last post by:
Hello friends, I have gone through various sites and understood what is structure padding, But no body tells it in the context of a microcontroller. In my view, for example in a 32 bit machine...
13
by: Roderik | last post by:
Hi, I am wondering why (half of the) squared images are not aligned on the right of the text in Internet Explorer (using <img ... align="right" />. I thought this was supported even in IE. In FF...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.