I am analyzing the code using Visual Studio tool and I get this message:
Object 'FileStream' can be disposed more than once in method 'BitmapFormat.WriteBitmap(byte[], int, int)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.
I tried to called disposed explicitly on the object but no avail.
This is the method:
Stream FileStream = null;
try
{
FileStream = File.Open("finger.bmp", FileMode.Create, FileAccess.Write);
BinaryWriter TmpBinaryWriter = new BinaryWriter(FileStream);
TmpBinaryWriter.Write(StructToBytes(BmpHeader, 14));
TmpBinaryWriter.Write(StructToBytes(BmpInfoHeader, Marshal.SizeOf(BmpInfoHeader)));
//µ÷ÊÔ°åÐÅÏ¢
for (ColorIndex = 0; ColorIndex < m_nColorTableEntries; ColorIndex++)
{
ColorMask[ColorIndex].redmask = (byte)ColorIndex;
ColorMask[ColorIndex].greenmask = (byte)ColorIndex;
ColorMask[ColorIndex].bluemask = (byte)ColorIndex;
ColorMask[ColorIndex].rgbReserved = 0;
TmpBinaryWriter.Write(StructToBytes(ColorMask[ColorIndex], Marshal.SizeOf(ColorMask[ColorIndex])));
}
//ͼƬÐýת£¬½â¾öÖ¸ÎÆÍ¼Æ¬µ¹Á¢µÄÎÊÌâ
RotatePic(buffer, nWidth, nHeight, ref ResBuf);
//дͼƬ
//TmpBinaryWriter.Write(ResBuf);
byte[] filter = null;
if (w - nWidth > 0)
{
filter = new byte[w - nWidth];
}
for (int i = 0; i < nHeight; i++)
{
TmpBinaryWriter.Write(ResBuf, i * nWidth, nWidth);
if (w - nWidth > 0)
{
TmpBinaryWriter.Write(ResBuf, 0, w - nWidth);
}
}
TmpBinaryWriter.Close();
FileStream.Close(); // <----- THE WARNING IS HERE
}
finally
{
if (FileStream != null)
FileStream.Dispose();
}
If I remove the try finally
block, the same happens. Even if I use using
statement.
How can I avoid it?