Home All Groups Group Topic Archive Search About

Image Resource - not released

Author
12 May 2006 1:49 AM
WayDownUnder
I am loading a image into a picture box using this line of code

_objPic.Image = System.Drawing.Image.FromFile(_fileName)

The picture box is then displayed on a form.
The file in _fileName is a temperary file.

After the form is closed down, I go back into the form and try and delete
the tempary file used in the previous display. the application errors with
cant delete rescource as it is used by another process.
On closing the first form I call the dispose method.
Nothing works until I close the application and start the process again.

any ideas


--
Life in the sun

Author
12 May 2006 7:37 AM
Gary Chang[MSFT]
Hi,

Thank you posting!

I am afraid this is a known issue. When you use the FromFile method to load
a PictureBox control with a picture file at run time, the  Visual Studio
.NET Integrated Development Environment (IDE) maintains a lock on the file.

To work around this problem, You need to avoid to use the FromFile method
to load picture file to a PictureBox control directly. The alternative
workarounds could be one of the following:

1. Use the FileStream object to load picture file insteadly:
// Make sure that you have added the System.IO namespace.
using System.IO;

// Specify a valid picture file path on your computer.
FileStream fs;
fs = new FileStream("C:\\WINNT\\Web\\Wallpaper\\Fly Away.jpg",
FileMode.Open, FileAccess.Read);
pictureBox1.Image = System.Drawing.Image.FromStream(fs);
fs.Close();

2. Use the Bitmap( x, y, pixelformat ) constructor to create a 32bpp
Bitmap, then Graphics.FromImage(), then draw your original Image on the new
one and Dispose() the Graphics. Please refer to the following newsgroup
thread:
http://groups.google.com/group/microsoft.public.dotnet.framework.drawing/bro
wse_frm/thread/9f22e459fcace234/0341a75ce3662728?lnk=st&q=System.Drawing.Ima
ge.FromFile+file+lock&rnum=8&hl=en#0341a75ce3662728

3. Use the Img.GetTumbnailImage to load image to the PictureBox control,
please refer to the following newsgroup thread for sample code:
http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_f
rm/thread/f3d2227be4476c01/f0103a542b89a1cb?lnk=st&q=System.Drawing.Image.Fr
omFile+file+lock&rnum=4&hl=en#f0103a542b89a1cb

I hope the above information helps, if you have any questions or concerns,
please do not hesitate to let me know. I am standing by to help you.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
12 May 2006 7:57 AM
WayDownUnder
Gary,
Number one works great thanks
--
Life in the sun


""Gary Chang[MSFT]"" wrote:

Show quoteHide quote
> Hi,
>
> Thank you posting!
>
> I am afraid this is a known issue. When you use the FromFile method to load
> a PictureBox control with a picture file at run time, the  Visual Studio
> .NET Integrated Development Environment (IDE) maintains a lock on the file.
>
> To work around this problem, You need to avoid to use the FromFile method
> to load picture file to a PictureBox control directly. The alternative
> workarounds could be one of the following:
>
> 1. Use the FileStream object to load picture file insteadly:
> // Make sure that you have added the System.IO namespace.
> using System.IO;
>
> // Specify a valid picture file path on your computer.
> FileStream fs;
> fs = new FileStream("C:\\WINNT\\Web\\Wallpaper\\Fly Away.jpg",
> FileMode.Open, FileAccess.Read);
> pictureBox1.Image = System.Drawing.Image.FromStream(fs);
> fs.Close();
>
> 2. Use the Bitmap( x, y, pixelformat ) constructor to create a 32bpp
> Bitmap, then Graphics.FromImage(), then draw your original Image on the new
> one and Dispose() the Graphics. Please refer to the following newsgroup
> thread:
> http://groups.google.com/group/microsoft.public.dotnet.framework.drawing/bro
> wse_frm/thread/9f22e459fcace234/0341a75ce3662728?lnk=st&q=System.Drawing.Ima
> ge.FromFile+file+lock&rnum=8&hl=en#0341a75ce3662728
>
> 3. Use the Img.GetTumbnailImage to load image to the PictureBox control,
> please refer to the following newsgroup thread for sample code:
> http://groups.google.com/group/microsoft.public.dotnet.languages.vb/browse_f
> rm/thread/f3d2227be4476c01/f0103a542b89a1cb?lnk=st&q=System.Drawing.Image.Fr
> omFile+file+lock&rnum=4&hl=en#f0103a542b89a1cb
>
> I hope the above information helps, if you have any questions or concerns,
> please do not hesitate to let me know. I am standing by to help you.
>
> Thanks!
>
> Best regards,
>
> Gary Chang
> Microsoft Online Community Support
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
>
Author
12 May 2006 9:35 AM
Gary Chang[MSFT]
I am glad to know it works. :)

Have a nice weekend!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
12 May 2006 11:39 AM
Herfried K. Wagner [MVP]
""Gary Chang[MSFT]"" <v-gar***@online.microsoft.com> schrieb:
> 1. Use the FileStream object to load picture file insteadly:
> // Make sure that you have added the System.IO namespace.
> using System.IO;
>
> // Specify a valid picture file path on your computer.
> FileStream fs;
> fs = new FileStream("C:\\WINNT\\Web\\Wallpaper\\Fly Away.jpg",
> FileMode.Open, FileAccess.Read);
> pictureBox1.Image = System.Drawing.Image.FromStream(fs);
> fs.Close();

Note that the stream the image object is based on must not be closed as long
as the image object is used!  Two solutions can be found here:

<URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
12 May 2006 11:55 PM
Dennis
Nice Tip...I'll use it.
--
Dennis in Houston


Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> ""Gary Chang[MSFT]"" <v-gar***@online.microsoft.com> schrieb:
> > 1. Use the FileStream object to load picture file insteadly:
> > // Make sure that you have added the System.IO namespace.
> > using System.IO;
> >
> > // Specify a valid picture file path on your computer.
> > FileStream fs;
> > fs = new FileStream("C:\\WINNT\\Web\\Wallpaper\\Fly Away.jpg",
> > FileMode.Open, FileAccess.Read);
> > pictureBox1.Image = System.Drawing.Image.FromStream(fs);
> > fs.Close();
>
> Note that the stream the image object is based on must not be closed as long
> as the image object is used!  Two solutions can be found here:
>
> <URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
>
>