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

Exception question, "File in Use"

Samishii23
246 100+
If loaded an image into my program, but needed to load it in again later, would I get an expection saying that the file is in use?
Dec 3 '09 #1
7 2439
GaryTexmo
1,501 Expert 1GB
There's a couple of discussions on this either on this page or the next one (a quick search should turn them up, try "image file in use" to show you how to get around this; however, as long as you're not trying to write or otherwise change the image file you should be able to load it normally multiple times and be just fine.

It puts a write-lock on the file, but read access should be ok... if I were you though, I'd run a quick test to see ;)
Dec 3 '09 #2
tlhintoq
3,525 Expert 2GB
Image.FromFile(string PathToFile) does indeed have a bug. It keeps a link to the file on HDD for as long as the image is being used by your program. In my opinion it should be avoided at all costs and I really wish MS would fix it since it has been a problem since framework 1.0

I recommend reading the file via a stream. Many examples exist on the net.


More importantly you might want to consider the architecture of your application. Ideally you should only have to load it one time. If you are loading multiple copies of it into different variables simultaneously then you are consuming a lot of memory for every duplicate instance of the image.
Dec 3 '09 #3
GaryTexmo
1,501 Expert 1GB
According to Microsoft, it's not a bug, it's a "feature" heh.

http://msdn.microsoft.com/en-us/library/stf701f5.aspx
See the "Remarks" section. Additionally, in my wanderings, I found a Microsoft support page saying it really was intended behaviour as well.

I suppose, in a way, it makes sense since the Image (and Bitmap) class allows you to save back to the file. The lock would prevent any external modifications to the file ensuring that the program with the image open will always have the correct copy, as well as the correct location of that image.

But would it really kill them to provide a flag saying, "Load this image into memory and release the file handle"...?

*Edit: Here's that link to a support page... http://support.microsoft.com/kb/309482
They call it a "problem" but further down they say, "This behavior is by design."

Last thing, and this is important... if the image you're loading is an animated image, you cannot close the stream as it will cause a general GDI+ exception to be thrown when the image is drawn. To get around this, load the file contents into a memory stream, close the file contents, then load the image from the memory which is then left open. Whenever you close a stream an animated image has been loaded from, you'll see the GDI+ exception. I don't know if this fits your situation, Samishii, but it's good to know about.
Dec 3 '09 #4
tlhintoq
3,525 Expert 2GB
Remarks
The file remains locked until the Image is disposed.
Personally, I don't think that is so much a statement saying it was INTENDED behavior - as an easy afterthought remark when they found out about the bug. By putting this on the webpage they didn't have to fix it.

After all, why would someone deliberatly keep the file locked? Or if that was a THOUGHT ABOUT feature they would have given a second parameter:

Expand|Select|Wrap|Line Numbers
  1. Image.FromFile(string PathToImage, bool LockAccess);
Dec 3 '09 #5
GaryTexmo
1,501 Expert 1GB
Again, I agree with you and that's how I would do it too, but either it's the case you describe or Microsoft actually does want it that way. As mentioned, I have my theories as to why they would want to do this, even if I don't agree with it. Unfortunately, Microsoft generally doesn't share their design decisions with the rest of us, which is too bad in the case of a framework like .NET. It would be nice to know where they are coming from once in a while.

If you caught my edit, from the second link...

This behavior is by design.
(Under the "status" section.)

Either way, it's easy enough to work around. Not add a parameter easy, but still easy ;)
Dec 3 '09 #6
Samishii23
246 100+
More importantly you might want to consider the architecture of your application. Ideally you should only have to load it one time. If you are loading multiple copies of it into different variables simultaneously then you are consuming a lot of memory for every duplicate instance of the image.
I was going to make an user option for my application that would allow them to load the image(s) into memory for quicker performance and higher memory usage, or use straight from files to avoid a large memory usage for users that don't have great computers.
Dec 4 '09 #7
GaryTexmo
1,501 Expert 1GB
I believe the image is loaded into memory anyway.
Dec 4 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: mike | last post by:
regards: I use Jtidy (api) to translate a HTML file into a "XHTML file". But The "XHTML file" cannot be identified by nokia 6600. Do I miss something important? Or this is Jtidy's weakness or...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
1
by: Erialc Berts | last post by:
I know why we get this error, but does anyone know how to catch it so that we can display a more helpful message to our users? I am interested both in a .exe and a web site. Thanks in advance...
2
by: FrzzMan | last post by:
The first time I called this function, everything went well, but the second time I called it. An Exception thrown, do you know why? An unhandled exception of type 'System.IO.IOException' occurred...
7
by: Drew Berkemeyer | last post by:
I've encounted a pretty strange problem and I'm not quite sure what to make of it. I have a web service that consumes an XML file as well as a few other parameters. This web service works fine...
3
by: SatCom | last post by:
Hello, I had originally posted this in the winforms.controls discussion, forgive the double post, Here is where I need help... I have been porting some VB6 to VB2005 and here is the issue with...
0
by: Mohammad Omer | last post by:
Hi, I am working on MFC base SDI application using vs2k5. When I close my application, exception shows up in "wincore.cpp" line 449, statement "lResult = CallWindowProc(oldWndProc, hWnd, nMsg,...
2
by: jjlagtap | last post by:
Hey everyone When I try to open a file i get the Exception listed below. The weird thing is it works when I run the web app locally and when i use a remote server and open a file on my computer....
11
by: MC | last post by:
I have a couple of customers who are using a program that deserializes an XML file, and on some files, the program fails to see the content of the file -- treats it as zero length and reports "root...
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
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: 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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.