如何擺脫“對(duì)象'對(duì)象'可以在方法中多次處理”
我正在使用 Visual Studio 工具分析代碼,并收到以下消息:對(duì)象 'FileStream' 可以在方法 'BitmapFormat.WriteBitmap(byte[], int, int)' 中多次處理。為避免生成 System.ObjectDisposedException,您不應(yīng)在一個(gè)對(duì)象上多次調(diào)用 Dispose。我試圖在對(duì)象上顯式調(diào)用dispose,但無濟(jì)于事。這是方法: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))); //μ÷ê?°?D??¢ 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]))); } //í???Dy×a£??a??????í???μ1á¢μ??êìa RotatePic(buffer, nWidth, nHeight, ref ResBuf); //D′í??? //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塊,也會(huì)發(fā)生同樣的情況。即使我使用using聲明。我怎樣才能避免它?
查看完整描述