我正在使用 Visual Studio 工具分析代码,并收到以下消息:
对象 'FileStream' 可以在方法 'BitmapFormat.WriteBitmap(byte[], int, int)' 中多次处理。为避免生成 System.ObjectDisposedException,您不应在一个对象上多次调用 Dispose。
我试图在对象上显式调用dispose,但无济于事。
这是方法:
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();
}
如果我删除try finally块,也会发生同样的情况。即使我使用using声明。
我怎样才能避免它?
潇湘沐
浮云间
侃侃无极
相关分类