2014-07-20 102 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); 
} 
相关问题