473,404 Members | 2,137 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,404 software developers and data experts.

PictureBox Control releasing files

Hello all,

I have an application that is using some picture box controls that are being
used to preview jpg files. The jpg files are created on the fly and will be
overwritten when needed. The issue I'm having is that when I release the
picturebox resources, they still seem to be hanging on at the OS level
because I can't delete the file.

Here's a code snippet of what I'm doing:

private void button1_Click(object sender, System.EventArgs e)
{
//check to see if the picturebox has an image already.
if(pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
if(System.IO.File.Exists("C:\\test.jpg"))
{
System.IO.File.Delete("C:\\test.jpg");
}
//Save the new file with the same name:
wwss.SaveAsJpeg("C:\\test.jpg");
pictureBox1.Image =
Image.FromFile(@"C:\\test.jpg").GetThumbnailImage( 136,120,null,new
intPtr());
}

Works first time through every time. The second time the button is clicked,
it goes into the check for image section just fine. No errors, until I get
to deleting the file. If I comment out the pictureBox1.Image =
Image.FromFile... section and trace through this same code, it'll run all
day long deleting the file and saving a new one. So where am I going wrong,
or is this some known issue that I'm not aware of ? Using .NET 1.0

Thanks!

Sean
Nov 15 '05 #1
2 12953
see comments below
"Sean" <sp******@earthlink.net> wrote in message
news:L1**************@newsread1.news.atl.earthlink .net...
Hello all,

I have an application that is using some picture box controls that are being used to preview jpg files. The jpg files are created on the fly and will be overwritten when needed. The issue I'm having is that when I release the picturebox resources, they still seem to be hanging on at the OS level because I can't delete the file.

Here's a code snippet of what I'm doing:

private void button1_Click(object sender, System.EventArgs e)
{
//check to see if the picturebox has an image already.
if(pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
if(System.IO.File.Exists("C:\\test.jpg"))
{
System.IO.File.Delete("C:\\test.jpg");
}
//Save the new file with the same name:
wwss.SaveAsJpeg("C:\\test.jpg");
pictureBox1.Image =
Image.FromFile(@"C:\\test.jpg").GetThumbnailImage( 136,120,null,new
intPtr());
}
The problem is that you have two image objects and you diposing the
wrong one. Let me show you in more detail:

This line: pictureBox1.Image =
Image.FromFile(@"C:\\test.jpg").GetThumbnailImage( 136,120,null,new
intPtr());
Is more or less the same as:
Image image1 = Image.FromFile(@"C:\\test.jpg")
Image image2 = image1.GetThumbnailImage(blah, blah)
pictureBox1.Image = image2;
image1 = null; // I don't believe there would be any references to
the original image, so it's like it's null here

However, just because image1 is "null" doesn't mean that it has been
disposed. I suppose if a GC happened to get it, you'd be fine. I'd
suggest the following:

using (Image image1 = Image.FromFile(@"C:\\test.jpg"))
{
pictureBox1.Image = image1.GetThumbnailImage(blah, blah)
} // note that dispose is automatically called as you leave this block

I didn't actually test it, cause I don't feel like implementing the
callback and all that garbage. You probably won't even need all your
other code as follows:
if(pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}


Let me know if there are any other issues.

regards,
Mike Mayer
Nov 15 '05 #2
Mike,

Worked like a champ! Thanks alot. Just needed another pair of eyes and had
a horrible brain block.

Sean

"Michael Mayer" <mr*****@charter.net> wrote in message
news:ug*************@TK2MSFTNGP10.phx.gbl...
see comments below
"Sean" <sp******@earthlink.net> wrote in message
news:L1**************@newsread1.news.atl.earthlink .net...
Hello all,

I have an application that is using some picture box controls that

are being
used to preview jpg files. The jpg files are created on the fly and

will be
overwritten when needed. The issue I'm having is that when I

release the
picturebox resources, they still seem to be hanging on at the OS

level
because I can't delete the file.

Here's a code snippet of what I'm doing:

private void button1_Click(object sender, System.EventArgs e)
{
//check to see if the picturebox has an image already.
if(pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}
if(System.IO.File.Exists("C:\\test.jpg"))
{
System.IO.File.Delete("C:\\test.jpg");
}
//Save the new file with the same name:
wwss.SaveAsJpeg("C:\\test.jpg");
pictureBox1.Image =
Image.FromFile(@"C:\\test.jpg").GetThumbnailImage( 136,120,null,new
intPtr());
}


The problem is that you have two image objects and you diposing the
wrong one. Let me show you in more detail:

This line:
pictureBox1.Image =
Image.FromFile(@"C:\\test.jpg").GetThumbnailImage( 136,120,null,new
intPtr());


Is more or less the same as:
Image image1 = Image.FromFile(@"C:\\test.jpg")
Image image2 = image1.GetThumbnailImage(blah, blah)
pictureBox1.Image = image2;
image1 = null; // I don't believe there would be any references to
the original image, so it's like it's null here

However, just because image1 is "null" doesn't mean that it has been
disposed. I suppose if a GC happened to get it, you'd be fine. I'd
suggest the following:

using (Image image1 = Image.FromFile(@"C:\\test.jpg"))
{
pictureBox1.Image = image1.GetThumbnailImage(blah, blah)
} // note that dispose is automatically called as you leave this block

I didn't actually test it, cause I don't feel like implementing the
callback and all that garbage. You probably won't even need all your
other code as follows:
if(pictureBox1.Image != null)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
}


Let me know if there are any other issues.

regards,
Mike Mayer

Nov 15 '05 #3

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

Similar topics

4
by: Ed Landau | last post by:
In VB6.0, I can put an image on a form and assign it's .picture property at run-time. In VBA (within MS Access), when I put down an image control on a form, it asks me for the source to the image...
3
by: Shailaja Kulkarni | last post by:
hi All, This problem is for C# windows application. I have a user control whcih contains one picture box along with other controls. Depending on certain conditions I want to change images on the...
6
by: Dick | last post by:
Hello, In my application I have a picturebox that must contain previews of picture files. The picturebox must always have the same size. For small images that fit, everything is ok. How can...
5
by: BrianW | last post by:
I am working on a program that has multiple picturebox controls that a user is allowed to move around which are contained within a panel control for visual placement. In my mousedown event, I set...
6
by: Rich | last post by:
Hello, I want to simulate the dynamic thumbnail display of Windows Explorer (winxp) on a form or pannel container. If I place a picture box on my container form/pannel and dimension it to the...
1
by: ray well | last post by:
hi, i need to give the user the ability to extract a rectangular area of their choice from a graphic displayed in a picture box to the clipboard, so they can use it elsewhere. say the graphic...
0
by: kimiraikkonen | last post by:
Hi, I noticed that if i view some jpeg files via Picturebox control (.net 2.0) placed in to a single form, while dragging the form around the screen, then the form slutters, dragging slows down...
3
by: kirk | last post by:
I have a form with a PictureBox control on it. The .Image property is set to a PNG file(which shows the picture of the US map) with some transparency in it. The .BackColor property is set to...
1
by: leshka82 | last post by:
I have recently designed a Winform Image drag & drop utility. The approach I took was to have a Panel control serve as a destination for multiple file drop. Once the user dragged & dropped the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.