[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.drawing

Confusion about Image object and PictureBox usage.

Paul Beard

10/19/2004 1:06:00 PM

I have an application that uses a PictureBox to display real-time video that
is captured from a JPEG video acquisition card. I basically get a JPEG
image buffer from the card that I convert to a .NET Bitmap object. I then
assign a clone of the Bitmap object to the Image property of the PictureBox.
This is done at about 30 images per second. The PictureBox displays the
consecutive images correctly. One thing that I am concerned with is that I
think that I am seeing a memory leak and I am not sure how to get around.

Each time I assign the image to the PictureBox I do this by calling the
Clone method. of the Image and assign it to the PictureBox.Image property.
The part that I am confused about is should I call the Dispose method of the
Image assigned to the PictureBox before I assign a new Image object to it or
will the Image object release its resources when it is garbage collected?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------


7 Answers

Allan McLemore

10/19/2004 1:21:00 PM

0

Hi Ken,

>I have an application that uses a PictureBox to display real-time video
>that
> is captured from a JPEG video acquisition card. I basically get a JPEG
> image buffer from the card that I convert to a .NET Bitmap object. I then
> assign a clone of the Bitmap object to the Image property of the
> PictureBox.
> This is done at about 30 images per second. The PictureBox displays the
> consecutive images correctly. One thing that I am concerned with is that
> I
> think that I am seeing a memory leak and I am not sure how to get around.

Assigning an image to an picture box should cause no memory leak at all,
the image object is disposed of when it has no references and falls out of
scope, as with almost all objects in .NET. You might find that your memory
leak is coming from elsewhere, possibly in the code that is converting the
image to a .NET bitmap object, have you tried simply letting the objects
fall out of scope instead of assigning them to the picturebox to see if your
memory leak still occurs?

> Each time I assign the image to the PictureBox I do this by calling the
> Clone method. of the Image and assign it to the PictureBox.Image
> property.
> The part that I am confused about is should I call the Dispose method of
> the
> Image assigned to the PictureBox before I assign a new Image object to it
> or
> will the Image object release its resources when it is garbage collected?

You shouldn't need to dispose of the image object, the Clone method is
used to create a new object with the same data in, so if the original object
changes your "clone" wont (that is my understanding anyway). How is your
JPEG getting converted to a .NET bitmap? Are you getting the image from a
COM library or is it all .NET code?

I had some code recently which obtained an image via COM and converted
it to .NET, I found a memory leak in the code as it did not release all of
the COM objects. I originally thought it might be the cloning of the image,
but I don't think it is possible as long as you aren't storing all of the
objects anywhere else so that they never fall out of scope? Anyway, hope
this helps.

Nick.


Paul Beard

10/19/2004 7:40:00 PM

0

At this point, I am not sure if I have a memory leak or not. The jury is
still out on that right now. I just wanted to make sure make sure that the
Dispose method call was not required to free up the bitmap resources. The
"memory leak" may have been the fact that the GC had not collected the
bitmap memory yet and I could have assumed a leak.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------
"Nak" <a@a.com> wrote in message
news:%23NgVt5dtEHA.1276@TK2MSFTNGP12.phx.gbl...
> Hi Ken,
>
> >I have an application that uses a PictureBox to display real-time video
> >that
> > is captured from a JPEG video acquisition card. I basically get a JPEG
> > image buffer from the card that I convert to a .NET Bitmap object. I
then
> > assign a clone of the Bitmap object to the Image property of the
> > PictureBox.
> > This is done at about 30 images per second. The PictureBox displays the
> > consecutive images correctly. One thing that I am concerned with is
that
> > I
> > think that I am seeing a memory leak and I am not sure how to get
around.
>
> Assigning an image to an picture box should cause no memory leak at
all,
> the image object is disposed of when it has no references and falls out of
> scope, as with almost all objects in .NET. You might find that your
memory
> leak is coming from elsewhere, possibly in the code that is converting the
> image to a .NET bitmap object, have you tried simply letting the objects
> fall out of scope instead of assigning them to the picturebox to see if
your
> memory leak still occurs?
>
> > Each time I assign the image to the PictureBox I do this by calling the
> > Clone method. of the Image and assign it to the PictureBox.Image
> > property.
> > The part that I am confused about is should I call the Dispose method of
> > the
> > Image assigned to the PictureBox before I assign a new Image object to
it
> > or
> > will the Image object release its resources when it is garbage
collected?
>
> You shouldn't need to dispose of the image object, the Clone method is
> used to create a new object with the same data in, so if the original
object
> changes your "clone" wont (that is my understanding anyway). How is your
> JPEG getting converted to a .NET bitmap? Are you getting the image from a
> COM library or is it all .NET code?
>
> I had some code recently which obtained an image via COM and converted
> it to .NET, I found a memory leak in the code as it did not release all of
> the COM objects. I originally thought it might be the cloning of the
image,
> but I don't think it is possible as long as you aren't storing all of the
> objects anywhere else so that they never fall out of scope? Anyway, hope
> this helps.
>
> Nick.
>
>


Allan McLemore

10/19/2004 8:04:00 PM

0

Hi Ken

One way you can test this is simply call the garbage collector yourself
after *not* using each image, if you simply aquire each JPG and turn it into
a .NET image, then let it fall out of scope and call GC.Collect you should
be able to see if that is the problem. Just an idea :-)

Nick.

"Ken Varn" <nospam> wrote in message
news:e8bbRNhtEHA.2788@TK2MSFTNGP09.phx.gbl...
> At this point, I am not sure if I have a memory leak or not. The jury is
> still out on that right now. I just wanted to make sure make sure that
> the
> Dispose method call was not required to free up the bitmap resources. The
> "memory leak" may have been the fact that the GC had not collected the
> bitmap memory yet and I could have assumed a leak.
>
> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.
>
> EmailID = varnk
> Domain = Diebold.com
> -----------------------------------
> "Nak" <a@a.com> wrote in message
> news:%23NgVt5dtEHA.1276@TK2MSFTNGP12.phx.gbl...
>> Hi Ken,
>>
>> >I have an application that uses a PictureBox to display real-time video
>> >that
>> > is captured from a JPEG video acquisition card. I basically get a JPEG
>> > image buffer from the card that I convert to a .NET Bitmap object. I
> then
>> > assign a clone of the Bitmap object to the Image property of the
>> > PictureBox.
>> > This is done at about 30 images per second. The PictureBox displays
>> > the
>> > consecutive images correctly. One thing that I am concerned with is
> that
>> > I
>> > think that I am seeing a memory leak and I am not sure how to get
> around.
>>
>> Assigning an image to an picture box should cause no memory leak at
> all,
>> the image object is disposed of when it has no references and falls out
>> of
>> scope, as with almost all objects in .NET. You might find that your
> memory
>> leak is coming from elsewhere, possibly in the code that is converting
>> the
>> image to a .NET bitmap object, have you tried simply letting the objects
>> fall out of scope instead of assigning them to the picturebox to see if
> your
>> memory leak still occurs?
>>
>> > Each time I assign the image to the PictureBox I do this by calling the
>> > Clone method. of the Image and assign it to the PictureBox.Image
>> > property.
>> > The part that I am confused about is should I call the Dispose method
>> > of
>> > the
>> > Image assigned to the PictureBox before I assign a new Image object to
> it
>> > or
>> > will the Image object release its resources when it is garbage
> collected?
>>
>> You shouldn't need to dispose of the image object, the Clone method
>> is
>> used to create a new object with the same data in, so if the original
> object
>> changes your "clone" wont (that is my understanding anyway). How is your
>> JPEG getting converted to a .NET bitmap? Are you getting the image from
>> a
>> COM library or is it all .NET code?
>>
>> I had some code recently which obtained an image via COM and
>> converted
>> it to .NET, I found a memory leak in the code as it did not release all
>> of
>> the COM objects. I originally thought it might be the cloning of the
> image,
>> but I don't think it is possible as long as you aren't storing all of the
>> objects anywhere else so that they never fall out of scope? Anyway, hope
>> this helps.
>>
>> Nick.
>>
>>
>
>


Bob Powell

10/19/2004 11:35:00 PM

0

It will do no harm to call dispose on the outgoing image. I would recommend
doing it if you can.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdipl...

The GDI+ FAQ RSS feed: http://www.bobpowell.net/f...
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tips...
Bob's Blog: http://bobpowelldotnet.blogspot.co...






"Ken Varn" <nospam> wrote in message
news:eYQ1%23wdtEHA.4040@TK2MSFTNGP09.phx.gbl...
> I have an application that uses a PictureBox to display real-time video
that
> is captured from a JPEG video acquisition card. I basically get a JPEG
> image buffer from the card that I convert to a .NET Bitmap object. I then
> assign a clone of the Bitmap object to the Image property of the
PictureBox.
> This is done at about 30 images per second. The PictureBox displays the
> consecutive images correctly. One thing that I am concerned with is that
I
> think that I am seeing a memory leak and I am not sure how to get around.
>
> Each time I assign the image to the PictureBox I do this by calling the
> Clone method. of the Image and assign it to the PictureBox.Image
property.
> The part that I am confused about is should I call the Dispose method of
the
> Image assigned to the PictureBox before I assign a new Image object to it
or
> will the Image object release its resources when it is garbage collected?
>
> --
> -----------------------------------
> Ken Varn
> Senior Software Engineer
> Diebold Inc.
>
> EmailID = varnk
> Domain = Diebold.com
> -----------------------------------
>
>


N Thorell

10/22/2004 12:49:00 PM

0

I have a problem that might be related.

When I re-create the image file for the PictureBox.Image, I call
...Image.Dispose(), but the file is still 'in use', when I try to
'File.Delete(..)' it.

Does the Image really tie up the jpg-file, and how can I stop that?

/Nils Thorell

"Bob Powell [MVP]" wrote:

> It will do no harm to call dispose on the outgoing image. I would recommend
> doing it if you can.
>
> --
> Bob Powell [MVP]
> Visual C#, System.Drawing
>
> Answer those GDI+ questions with the GDI+ FAQ
> http://www.bobpowell.net/gdipl...
>
> The GDI+ FAQ RSS feed: http://www.bobpowell.net/f...
> Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tips...
> Bob's Blog: http://bobpowelldotnet.blogspot.co...
>
>
>
>
>
>
> "Ken Varn" <nospam> wrote in message
> news:eYQ1%23wdtEHA.4040@TK2MSFTNGP09.phx.gbl...
> > I have an application that uses a PictureBox to display real-time video
> that
> > is captured from a JPEG video acquisition card. I basically get a JPEG
> > image buffer from the card that I convert to a .NET Bitmap object. I then
> > assign a clone of the Bitmap object to the Image property of the
> PictureBox.
> > This is done at about 30 images per second. The PictureBox displays the
> > consecutive images correctly. One thing that I am concerned with is that
> I
> > think that I am seeing a memory leak and I am not sure how to get around.
> >
> > Each time I assign the image to the PictureBox I do this by calling the
> > Clone method. of the Image and assign it to the PictureBox.Image
> property.
> > The part that I am confused about is should I call the Dispose method of
> the
> > Image assigned to the PictureBox before I assign a new Image object to it
> or
> > will the Image object release its resources when it is garbage collected?
> >
> > --
> > -----------------------------------
> > Ken Varn
> > Senior Software Engineer
> > Diebold Inc.
> >
> > EmailID = varnk
> > Domain = Diebold.com
> > -----------------------------------
> >
> >
>
>
>

N Thorell

10/22/2004 1:43:00 PM

0



"N Thorell" wrote:

> I have a problem that might be related.
>
> When I re-create the image file for the PictureBox.Image, I call
> ..Image.Dispose(), but the file is still 'in use', when I try to
> 'File.Delete(..)' it.
>
> Does the Image really tie up the jpg-file, and how can I stop that?
>
> /Nils Thorell
>
> "Bob Powell [MVP]" wrote:
>
> > It will do no harm to call dispose on the outgoing image. I would recommend
> > doing it if you can.
> >
> > --
> > Bob Powell [MVP]
> > Visual C#, System.Drawing
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdipl...
> >
> > The GDI+ FAQ RSS feed: http://www.bobpowell.net/f...
> > Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tips...
> > Bob's Blog: http://bobpowelldotnet.blogspot.co...
> >
> >
> >
> >
> >
> >
> > "Ken Varn" <nospam> wrote in message
> > news:eYQ1%23wdtEHA.4040@TK2MSFTNGP09.phx.gbl...
> > > I have an application that uses a PictureBox to display real-time video
> > that
> > > is captured from a JPEG video acquisition card. I basically get a JPEG
> > > image buffer from the card that I convert to a .NET Bitmap object. I then
> > > assign a clone of the Bitmap object to the Image property of the
> > PictureBox.
> > > This is done at about 30 images per second. The PictureBox displays the
> > > consecutive images correctly. One thing that I am concerned with is that
> > I
> > > think that I am seeing a memory leak and I am not sure how to get around.
> > >
> > > Each time I assign the image to the PictureBox I do this by calling the
> > > Clone method. of the Image and assign it to the PictureBox.Image
> > property.
> > > The part that I am confused about is should I call the Dispose method of
> > the
> > > Image assigned to the PictureBox before I assign a new Image object to it
> > or
> > > will the Image object release its resources when it is garbage collected?
> > >
> > > --
> > > -----------------------------------
> > > Ken Varn
> > > Senior Software Engineer
> > > Diebold Inc.
> > >
> > > EmailID = varnk
> > > Domain = Diebold.com
> > > -----------------------------------
> > >
> > >
> >
> >
> >

N Thorell

10/22/2004 1:45:00 PM

0

See:

http://www.bobpowell.net/f...

"N Thorell" wrote:

> I have a problem that might be related.
>
> When I re-create the image file for the PictureBox.Image, I call
> ..Image.Dispose(), but the file is still 'in use', when I try to
> 'File.Delete(..)' it.
>
> Does the Image really tie up the jpg-file, and how can I stop that?
>
> /Nils Thorell
>
> "Bob Powell [MVP]" wrote:
>
> > It will do no harm to call dispose on the outgoing image. I would recommend
> > doing it if you can.
> >
> > --
> > Bob Powell [MVP]
> > Visual C#, System.Drawing
> >
> > Answer those GDI+ questions with the GDI+ FAQ
> > http://www.bobpowell.net/gdipl...
> >
> > The GDI+ FAQ RSS feed: http://www.bobpowell.net/f...
> > Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tips...
> > Bob's Blog: http://bobpowelldotnet.blogspot.co...
> >
> >
> >
> >
> >
> >
> > "Ken Varn" <nospam> wrote in message
> > news:eYQ1%23wdtEHA.4040@TK2MSFTNGP09.phx.gbl...
> > > I have an application that uses a PictureBox to display real-time video
> > that
> > > is captured from a JPEG video acquisition card. I basically get a JPEG
> > > image buffer from the card that I convert to a .NET Bitmap object. I then
> > > assign a clone of the Bitmap object to the Image property of the
> > PictureBox.
> > > This is done at about 30 images per second. The PictureBox displays the
> > > consecutive images correctly. One thing that I am concerned with is that
> > I
> > > think that I am seeing a memory leak and I am not sure how to get around.
> > >
> > > Each time I assign the image to the PictureBox I do this by calling the
> > > Clone method. of the Image and assign it to the PictureBox.Image
> > property.
> > > The part that I am confused about is should I call the Dispose method of
> > the
> > > Image assigned to the PictureBox before I assign a new Image object to it
> > or
> > > will the Image object release its resources when it is garbage collected?
> > >
> > > --
> > > -----------------------------------
> > > Ken Varn
> > > Senior Software Engineer
> > > Diebold Inc.
> > >
> > > EmailID = varnk
> > > Domain = Diebold.com
> > > -----------------------------------
> > >
> > >
> >
> >
> >