2014-07-20 146 views
0

我的加載RAW圖像的函數有問題。我不明白錯誤的原因。它顯示了我這個錯誤:不能隱式地將類型'int'轉換爲LittleEndian ByteOrder

Cannot implicitly convert type 'int' to 'Digital_Native_Image.DNI_Image.Byteorder'

public void load_DNI(string ficsrc) 
{ 
    FileStream FS = null; 
    BinaryReader BR = null; 
    int width = 0; 
    int height = 0; 
    DNI_Image.Datatype dataformat = Datatype.SNG; 
    DNI_Image.Byteorder byteformat = Byteorder.LittleEndian; 

    FS = new FileStream(ficsrc, System.IO.FileMode.Open); 
    BR = new BinaryReader(FS); 

    dataformat = BR.ReadInt32(); 
    byteformat = BR.ReadInt32(); 
    width = BR.ReadInt32(); 
    height = BR.ReadInt32(); 

    // fermeture des fichiers 
    if ((BR != null)) 
     BR.Close(); 
    if ((FS != null)) 
     FS.Dispose(); 

    // chargement de l'_image 
    ReadImage(ficsrc, width, height, dataformat, byteformat); 
} 

回答

1

int的不能被隱式轉換爲enum的。您需要在此處添加明確的演員表:

dataformat = (Datatype.SNG)BR.ReadInt32(); 
byteformat = (Byteorder)BR.ReadInt32(); 

閱讀Casting and Type Conversions (C# Programming Guide)瞭解更多信息。

但是,請注意,if (BR != null)檢查不是必需的,這實際上不是處理IDisposable對象的正確方法。我建議你重寫這段代碼以使用using blocks。這將確保FSBR得到妥善處置:

int width; 
int height; 
Datatype dataformat; 
Byteorder byteformat; 

using (var FS = FileStream(ficsrc, System.IO.FileMode.Open)) 
using (var BR = new BinaryReader(FS)) 
{ 

    dataformat = (Datatype.SNG)BR.ReadInt32(); 
    byteformat = (Byteorder.LittleEndian)BR.ReadInt32(); 
    width = BR.ReadInt32(); 
    height = BR.ReadInt32(); 
} 

// chargement de l'_image 
ReadImage(ficsrc, width, height, dataformat, byteformat); 

但是,它也好像你可以改善這種更通過重構的ReadImage方法使用相同的BinaryReader。那麼你可以重寫這種方法看起來更像這樣:

using (var FS = FileStream(ficsrc, System.IO.FileMode.Open)) 
using (var BR = new BinaryReader(FS)) 
{ 

    var dataformat = (Datatype.SNG)BR.ReadInt32(); 
    var byteformat = (Byteorder.LittleEndian)BR.ReadInt32(); 
    var width = BR.ReadInt32(); 
    var height = BR.ReadInt32(); 
    ReadImage(ficsrc, width, height, dataformat, byteformat, BR); 
} 
相關問題