473,378 Members | 1,355 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,378 software developers and data experts.

Image Resource - not released

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
May 12 '06 #1
5 1404
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/micro...rk.drawing/bro
wse_frm/thread/9f22e459fcace234/0341a75ce3662728?lnk=st&q=System.Drawing.Ima
ge.FromFile+file+lock&rnum=8&hl=en#0341a75ce366272 8

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/micro...es.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.
May 12 '06 #2
Gary,
Number one works great thanks
--
Life in the sun
""Gary Chang[MSFT]"" wrote:
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/micro...rk.drawing/bro
wse_frm/thread/9f22e459fcace234/0341a75ce3662728?lnk=st&q=System.Drawing.Ima
ge.FromFile+file+lock&rnum=8&hl=en#0341a75ce366272 8

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/micro...es.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.

May 12 '06 #3
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.
May 12 '06 #4
""Gary Chang[MSFT]"" <v-******@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/>

May 12 '06 #5
Nice Tip...I'll use it.
--
Dennis in Houston
"Herfried K. Wagner [MVP]" wrote:
""Gary Chang[MSFT]"" <v-******@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/>

May 13 '06 #6

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

Similar topics

5
by: John Perks and Sarah Mount | last post by:
When handling resources in Python, where the scope of the resource is known, there seem to be two schools of thought: (1) Explicit: f = open(fname) try: # ... finally: f.close()
1
by: D. Yates | last post by:
Hi, I am looking for an example of how to extract bitmap images from an embedded resource file (a file with *.res extension, which can be viewed inside of the ide and can hold bitmaps, icons,...
3
by: UJ | last post by:
I've got an image I want to embed in a dll to use on a screen later. I've got it in a resource file, got it to compile in to the dll. The problem is getting it back out. It seems like my problem is...
3
by: bbrewder | last post by:
I am interested in hearing how other people have handled sharing resources in multiple projects (for example, a save icon). We have a product that has many forms within many projects. Many of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.