Things that you didn’t know

by Miha Markič 22. October 2009 15:42

I am working on a some sort of file cache system lately and I’ve encountered a bizarre error. When a certain file representing an image had to be deleted it wouldn’t let it – an IOException was thrown stating:

The process cannot access the file '[FILEPATH]' because it is being used by another process.

The odd thing is that I’ve used this file only for retrieve images from and I had no idea why would it be locked. Eventually I’ve traced the lock down to this line:

Image result = Image.FromFile(path);

Would you say that the line above would lock a file? I wouldn’t. But it does for some obscure reason. Thus switching to Image.FromStream did the trick:

using (FileStream stream = File.OpenRead(path))
{
    return Image.FromStream(stream);
}

It is more lines but it works as expected.

IMPORTANT UPDATE: The workaround described above only seems to work - it crashes randomly. By reading documentation (who does that anyway? - thanks Dario for the hint Smile) it states that the stream has to open for the lifetime of the Image. Ouch. So here is the second workaround that works for sure - I copy the data to a MemoryStream and create image from there. It is even more lines but this time it has to work (there is a Stream.CopyTo method in forthcomming .net 4.0).

using (FileStream stream = File.OpenRead(path))
{
    MemoryStream imageDataStream = new MemoryStream(Convert.ToInt32(stream.Length));
    byte[] buffer = new byte[4096];
    int read;
    while ((read = stream.Read(buffer, 0, buffer.Length)) != 0)
    {
        imageDataStream.Write(buffer, 0, read);
    }
    imageDataStream.Position = 0;
    return Image.FromStream(imageDataStream);
}

Tags:

.net

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Miha Markic

About me
Righthand
 
Microsoft MVP
 
Developer Express' DXSquad
INETA Country Leader for Slovenia
INETA Country Leader for Slovenia

Slovene Developer Users Group Lead
Friends of Red-Gate
LLBLGenPro Partner

Miha currently works as a free lance consultant and software developer specialized in .net area.
He graduated in Computer and information science at the University of Ljubljana, Slovenia. He has accumulated experience in various programming languages such as Java, Visual Basic 3-6 (MCP), Visual C++, Delphi, C# and VB.Net through years.
He has experience in practically all (technical) stages of project development, including planning, framework development, user interface, business processes, as well as testing and documenting. He has worked on big and small projects in Slovenia and abroad (e.g. participated in completing level 3 IS for the Nucor steel plant, Hertford, USA).
Currently he enjoys programming in .net environment using C#. Since 2000 he has been active in Developer Express' DX Squad and has been ECDL trainer and tester. He also gives lectures on conferences and other events in Slovenia.

Month List

Tag cloud

Most comments

Paulius Paulius
1 comments
us United States
Meh Meh
1 comments
us United States
bart dm bart dm
1 comments
nl Netherlands

RecentComments

Comment RSS