C# 无法隐式转换类型';int';小印度字节顺序
我加载原始图像的函数有问题。我不明白错误的原因。它向我显示了以下错误: 无法将类型“int”隐式转换为“Digital\u Native\u Image.DNI\u Image.Byteorder”C# 无法隐式转换类型';int';小印度字节顺序,c#,binaryreader,C#,Binaryreader,我加载原始图像的函数有问题。我不明白错误的原因。它向我显示了以下错误: 无法将类型“int”隐式转换为“Digital\u Native\u Image.DNI\u Image.Byteorder” int不能隐式转换为enum。您需要在此处添加显式强制转换: dataformat = (Datatype.SNG)BR.ReadInt32(); byteformat = (Byteorder)BR.ReadInt32(); 阅读了解更多信息 但是,请注意,if(BR!=null)检查是不必要的
int
不能隐式转换为enum
。您需要在此处添加显式强制转换:
dataformat = (Datatype.SNG)BR.ReadInt32();
byteformat = (Byteorder)BR.ReadInt32();
阅读了解更多信息
但是,请注意,if(BR!=null)
检查是不必要的,这确实不是处理对象的正确方法。我建议您重写此代码以便使用。这将确保FS
和BR
得到正确处置:
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);
}
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);
}