0t3ch / aforge

Automatically exported from code.google.com/p/aforge
Other
0 stars 0 forks source link

File locked in AForge.Imaging.Formats.ImageDecoder.DecodeFromFile #192

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. try to load a bmp file with 
AForge.Imaging.Formats.ImageDecoder.DecodeFromFile
2.Don't stop the program
3.Try to delete the file. It's locked.

What is the expected output? What do you see instead?
The file can be deleted

What version of the product are you using?
I try with the last 2.1.5.

Please provide any additional information below.
The .bmp doesn't use the IImageDecoder provided but the bitmap = (Bitmap) 
Image.FromFile( fileName ); code that has a bug that lock the file.
Use the above code instead of  Image.FromFile :
System.IO.FileStream fs = new System.IO.FileStream(fileName , 
System.IO.FileMode.Open, System.IO.FileAccess.Read);
try
{
    bitmap  = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(fs);
}
finally
{
    fs.Close();

}

Original issue reported on code.google.com by philippe...@free.fr on 28 Mar 2011 at 4:19

GoogleCodeExporter commented 9 years ago
I found that the image must be copied before the stream is closed.
A better code should be :
System.IO.FileStream fs = new System.IO.FileStream(fileName , 
System.IO.FileMode.Open, System.IO.FileAccess.Read);
try
{
    bitmap  = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(fs);
    System.Drawing.Bitmap pic1 = new System.Drawing.Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(pic1);
    g.DrawImage(bitmap, 0, 0);
    g.Dispose();
    bitmap  = pic1;
}
finally
{
    fs.Close();

}

Original comment by philippe...@free.fr on 31 Mar 2011 at 7:56

GoogleCodeExporter commented 9 years ago
We already had working solution in AForge.Imaging.Image.FromFile():

        public static System.Drawing.Bitmap FromFile( string fileName )
        {
            Bitmap loadedImage = null;
            FileStream stream = null;

            try
            {
                // read image to temporary memory stream
                stream = File.OpenRead( fileName );
                MemoryStream memoryStream = new MemoryStream( );

                byte[] buffer = new byte[10000];
                while ( true )
                {
                    int read = stream.Read( buffer, 0, 10000 );

                    if ( read == 0 )
                        break;

                    memoryStream.Write( buffer, 0, read );
                }

                loadedImage = (Bitmap) Bitmap.FromStream( memoryStream );
            }
            finally
            {
                if ( stream != null )
                {
                    stream.Close( );
                    stream.Dispose( );
                }
            }

            return loadedImage;
        }

Original comment by andrew.k...@gmail.com on 31 Mar 2011 at 7:40

GoogleCodeExporter commented 9 years ago
Updated ImageDecoder.DecodeFromFile() to load load standard image formats (BMP, 
JPG, PNG, etc.) through memory stream instead of using standard .NET's 
FromFile( string fileName ) to avoid locking of the file.

Committed in revision 1406. Will be release in version 2.1.6.

Original comment by andrew.k...@gmail.com on 31 Mar 2011 at 7:45

GoogleCodeExporter commented 9 years ago

Original comment by andrew.k...@gmail.com on 28 Jul 2011 at 9:45

GoogleCodeExporter commented 9 years ago

Original comment by andrew.k...@gmail.com on 10 Aug 2011 at 9:26