Closed GoogleCodeExporter closed 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
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
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
Original comment by andrew.k...@gmail.com
on 28 Jul 2011 at 9:45
Original comment by andrew.k...@gmail.com
on 10 Aug 2011 at 9:26
Original issue reported on code.google.com by
philippe...@free.fr
on 28 Mar 2011 at 4:19